|

How to Install and Use R on Ubuntu

Brief: This tutorial teaches you to install R on Ubuntu. You’ll also learn how to run your first R program in Ubuntu using various methods.

R, together with Python, is the most commonly used programming language for statistical computing and graphics, making it easy to work with data. With the growing interest in data analysis, data visualization, data science (the machine learning craze), it is now more popular than ever and is a great tool for anyone looking to dive into this fields.

The good thing about R is that its syntax is pretty straight-forward and you can find many tutorials/guides on how R is used in the real world.

In this article, I’ll cover how to install R on Ubuntu Linux. I’ll also show you how to run your first R program in Linux.

Install R On Ubuntu

Installing R on Ubuntu

R is included in the Ubuntu repositories. It can be easily installed using:

sudo apt install r-base

Do note that this may install a slightly older version. At the time of writing this article, Ubuntu offers version 3.4 whereas the latest is version 3.6.

I advise sticking with whichever version Ubuntu provides unless you must use the newer version.

In order to get the latest version (or any specific version for that matter), you must use CRAN (The Comprehensive R Archive Network). This is a list of mirrors for downloading the latest version of R. Click on the next section to learn how to install the latest version of R on Ubuntu.

How to install latest R version 3.6 on Ubuntu (click to expand)

To get the R version 3.6, you need to add the mirror to your sources list. I have simplified it for you in this command:

sudo add-apt-repository "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran35/"

Now you should add the key for the repository:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

And then update the repository information and install R:

sudo apt update
sudo apt install r-base

That’s it.

Using R programming on Ubuntu

R has more than one use. I’ll go over several methods you can use to run R programs.

Interactive Mode in R

After having installed R, you can run the console using:

R

This should open up the interactive mode:

R Interactive Mode
R Interactive Mode

This R console is very similar to the Python and Haskell interactive prompts. You can enter any R command and you can do basic mathematical computations. For example:

> 20+40
[1] 60

> print ("Hello World!")
[1] "Hello World!"

You could test plotting too:

R Plotting
R Plotting

You can quit using q() or pressing CTRL+c. When doing so, you will be asked if you want to save a workspace image; a workspace is an environment for created variables.

Running R program with Rscript

The second way to run R programs is in directly on the Linux command line. You can do so using RScript, a utility included with r-base.

First, you have to save your R program to a file using your favorite code editor on Linux. The file extension should be .r.

This is my sample R program printing “Hello World”. I have saved it in a file name hello.r.

print("Hello World!")
a <- rnorm(100)
plot(a)

To run the R program, use the command like this:

Rscript hello.r

You should get back the output:

[1] "Hello World!"

The plot is going to be saved in the working directory, to a file named Rplots.pdf:

Rplots.pdf
Rplots.pdf

Note: Rscript doesn’t load the methods package by default. Make sure to load it explicitly in your script.

Run R scripts with RStudio in Ubuntu

The most common way to use R is using RStudio, a great cross-platform open source IDE. You can install it using deb file in Ubuntu. Download the deb file from the link below. You’ll have to scroll down a bit to locate the DEB files for Ubuntu.

Once you download the DEB file, just double click on it to install it.

Once installed, search for it in the menu and start it. The home window of the application should pop up:

RStudio Home
RStudio Home

Here you have a working console, just like the one you got in the terminal with the R command.

To create a file, in the top bar click on File and select New File > Rscript (or CTRL+Shift+n):

RStudio New File
RStudio New File

Press CTRL+s to save the file and choose a location and a name it:

RStudio Save File
RStudio Save File

After doing so, click on Session > Set Working Directory > To Source File Location to change the working directory to the location of your script:

RStudio Working Directory
RStudio Working Directory

You are now ready to go! Write in your code and click run. You should be able to see output both in the console and in the plotting window:

RStudio Run
RStudio Run

Wrapping Up

In this article, I showed you step by step how to get started using the R programming language on an Ubuntu system. I covered several ways you can go about this: R console – useful for testing, Rscript – for the terminal lover, RStudio – the IDE for your needs.

Whether you are willing to get into data science or simply love statistics, R is a good addition to your programming arsenal, being the perfect tool for analyzing data.

If you are absolutely new to R, let me recommend you this excellent book that will teach you fundamentals of R. It’s available on Amazon Kindle.

We earn a commission if you make a purchase, at no additional cost to you.
09/30/2022 09:54 pm GMT

Do you use R? Are you just getting into it? Let us know more about how and why you use or want to learn to use R!

Similar Posts

  • If memory serves, (oop, that’s a bad way to start), but I am trying my best to follow Sam Harris’s “Lying” – Not having even seen the 1975(?) precursor “radical honesty” I’d have to say that Sam’s book is way better. That makes sense, right? Well, if you read or listen (suggested listen to author’s own reading, I believe he disliked the original audio version so much he redid it himself and added a comment or two, again if memory…) – but it’s very short, and can probably be summed up in a paragraph. Except for he goes into dealing with difficulties that will undoubtedly pop up, since the *vast* majority of the human race lie regularly… anyway, sorry..) That Editor choice thing above, I didn’t even know there was a VSCodium editor (around 117MB and I have two VSCode’s listed one just says “insiders” at the end – 155MB and the non-“insiders” even has same website URL is 153.6MB and some say “lightweight” or “flexible and light” but if that’s light what’s something at 19MB or gnote under 10, I think.. oh ya.. memory..
    I believe Slackware was the first distro, I don’t recall having another option when I first installed with kernel 0.98 or v0.99.. but DL onto 35 3.5″ “floppies” maybe I didn’t look hard for another to try.. and I had to compile the Kernel every time I reinstalled made changes that the modules do now, I guess.

    Thanks for this, tho. Just what I wanted, quick and to the point. I’ve programmed BASIC on a Commodore PET where you save files to a cassette tape, C & C++ need mention? COBOL (yuk) IBM Mainframe assembly (YUK!) all the usual suspects till a few years back. Pascal, Lisp, Prolog, and a dozen or two others.. And about that “make install” thing – tried that in the kernel source? 6 million questions about how you want your kernel configured later.. and voila!

    oh 6502 assembly is Awesome!

    but anyway.. never (yet!) R. So here ‘goes… Thanks!