Not using Debian 9? Choose a different version:

Introduction

R is an open-source programming language that specializes in statistical computing and graphics. Supported by the R Foundation for Statistical Computing, it is widely used for developing statistical software and performing data analysis. An increasingly popular and extensible language with an active community, R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.

Step 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 9 cloud image.

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

sudo apt 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 install software-properties-common

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

sudo apt install apt-transport-https

With these dependencies in place, we’re ready to install R.

Step 2 — Installing R

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

Note: CRAN maintains the repositories within their network, but not all external repositories are reliable. Be sure to install only from trusted sources.

If the lines above appear in the output from the update command, we’ve successfully added the repository. We can be sure we won’t accidentally install an older version.

At this point, we’re ready to install R with the following command.

sudo apt install r-base

If prompted to confirm installation, press y to continue.

As of the time of writing, the latest stable version of R from CRAN is 3.5.1, which is displayed when you start R.

Since we’re planning to install an example package for every user on the system, we’ll start R as root so that the libraries will be available to all users automatically. Alternatively, if you run the R command without sudo, a personal library can be set up for your user.

This confirms that we’ve successfully installed R and entered its interactive shell.

Step 3 — Installing R Packages from CRAN

Part of R’s strength is its 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:

If you are 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(). To learn more about what’s available, you can find a listing of official packages organized by name via the Available CRAN Packages By Name list.

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

Conclusion

With R successfully installed on your server, you may be interested in this guide on installing the RStudio Server to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a Shiny server to convert your R code into interactive web pages.