Dayalan Saravanan


Install R in Debian 10 and add packages from the CRAN

Friday, June 19, 2020

Instructions for installing the latest R version in Debian 10 and add packages from the offical Comprehensive R Archive Network (CRAN).

  1. Installing Dependencies:

    Because R is a fast-moving project, the latest stable version isn't always available from Debian's repositories, so we'll need to add the external repository maintained by CRAN. In order to do this, we'll need to install some dependencies for the Debian 10.

    To perform network operations that manage and download certificates, we need to install dirmngr so that we can add the external repository.

    $ sudo apt-get install dirmngr --install-recommends

    To add a PPA reference to Debian, we'll need to use the add-apt- repository command. For installations where this command may not available, you can add this utility to your system by installing software-properties-common:

    $ sudo apt-get install software-properties-common

    Finally, to ensure that we have HTTPS support for secure protocols, we'll install the following tool:

    $ sudo apt-get install apt-transport-https

  2. Installing R:

    For the most recent version of R, we'll be installing from the CRAN repositories.

    Let's first add the relevant GPG key.

    $ sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C@AD5F960A256A04AF'

    Once we have the trusted key, we can add the repository. Note that if you're not using Debian 10 (Buster), you can look at the supported R Project Debian branches, named for each release.

    $ sudo add-apt-repository 'deb http://cloud.r-project.org/bin/linux/debian buster-cran35/'

    Now, we'll need to run update after this in order to include package manifests from the new repository.

    $ sudo apt-get update

    Once this completes running and you're returned to your prompt, we're ready to install R with the following command.

    $ sudo apt-get install r-base

    If prompted to confirm installation, press y to continue.

    To install a package for every user on the system, start R as root so that the libraries will be available to all users automatically. Similarly, if you run the R command without sudo, a personal library can be set up for the user.

    $ sudo -i R

  3. Installing R Packages from CRAN:

    Part of R's strength is it available abundance of add-on packages. For demonstration purposes, we'll install txtplot, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:

    > install.packages('txtplot')

    When the installation is complete, we can load txtplot:

    > library('txtplot')

    If you're interested to learn more about txtplot, use help(txtplot) from within the R interpreter.

    Any precompiled package can be installed from CRAN with install. packages(). Find a listing of official packages organized by name via the CRAN packages by name list.

    To quit R, you can type q(). Unless you want to save the workspace image, you can press n.


  4. Updating packages:

    The command update.packages() is the simplest way to ensure that all the packages on your system are up to date. It downloads the list of available packages and their current versions, compares it with those installed and offers to fetch and install any that have later versions on the repositories.

    An alternative interface to keeping packages up-to-date is provided by the command packageStatus(), which returns an object with information on all installed packages and packages available at multiple repositories. The print and summary methods give an overview of installed and available packages, the upgrade method offers to fetch and install the latest versions of outdated packages.

    One sometimes-useful additional piece of information that packageStatus() returns is the status of a package, as "ok", "upgrade" or "unavailable" (in the currently selected repositories). For example

    > inst <- packageStatus()$inst 
    inst[inst$Status != "ok", c("Package", "Version", "Status")]

  5. Removing packages:

    From a running R process they can be removed by

    > remove.packages(c("pkg1", "pkg2"), lib = file.path("path/to/library"))

    Finally, one can just remove the package directory from the library.


Resources for R programming:

The official intro, "An Introduction to R", available online in html version.