Introduction
R is an open-source programming language, that is widely used for developing statistical software and for performing data analysis and visualization. R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.
Prerequisites
To follow along with this tutorial, you will need a Debian 10 server with:
- At least 1GB of RAM
- A non-root user with sudo privileges
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 Debian.
To perform network operations that manage and download certificates, we need to install dirmngr
so that we can add the external repository.
sudo apt update
sudo apt install dirmngr --install-recommends
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.
Let’s first add the relevant GPG key.
sudo gpg --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'
sudo gpg --armor --export '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7' | sudo tee /etc/apt/trusted.gpg.d/cran_debian_key.asc
Once we have the trusted key, we can add the repository.
-
Debian 11
echo "deb http://cloud.r-project.org/bin/linux/debian bullseye-cran40/" | sudo tee -a /etc/apt/sources.list
-
Debian 12
echo "deb http://cloud.r-project.org/bin/linux/debian bookworm-cran40/" | sudo tee -a /etc/apt/sources.list
Now, we’ll need to run an update after this in order to include package manifests from the new repository.
sudo apt update
Once this completes running and you’re returned to your prompt, check if the latest version of r-base
is now available. If it is, we’re ready to install R with the following command.
sudo apt-cache policy r-base
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 4.4.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.
sudo -i R
This confirms that we’ve successfully installed R and entered its interactive shell.
Step 3 - Installing Add-on 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:
install.packages('txtplot')
When the installation is complete, we can load txtplot
:
library('txtplot')
If there are no error messages, the library has successfully loaded. Let’s put it in action now with an example which demonstrates a basic plotting function with axis labels. The example data, supplied by R's datasets
package, contains the speed of cars and the distance required to stop based on data from the 1920s:
txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')
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
By following this tutorial you have successfully installed the programming language R on your server, and know how to install additional add-ons.