Run R scripts from the command line on Ubuntu

Running R scripts from the command line can be a powerful way to:

  • Automate your R scripts
  • Integrate R into production
  • Call R through other tools or systems
There are basically two Linux commands that are used:
  1. RScript (preferred)
  2. The older command is  R CMD BATCH.

A better way to run R scripts in batch mode is Rscript, and its comes with R.

See the example below to see the difference between using RScript and R CMD BATCH.

Save 

print("hello world")

as a r script file and name it helloworld.r, and then run it in your terminal:

(Be sure to first cd to the path where you saved the helloworld.r file.)

then type the commands (the lines below in bold) to your temrinal

$ Rscript helloworld.r
[1] "hello world"
$ R CMD BATCH helloworld.r
$

We can see that Rscript directly  output to the terminal , and R CMD BATCH has done nothing. But actually, R CMD BATCH has written its output to a file called helloworld.r.Rout(it is located at the same place where you put helloworld.r), and that output includes both the commands and output, just like in interactive mode, along with some runtime stats:

> print("hello world")
[1] "hello world"
> 
> 
> proc.time()
   user  system elapsed 
  0.080   0.004   0.113

You can call these directly from the command line or integrate them into a bash script. You can also call these from any job scheduler.

Note, these are R related tools. The RStudio IDE does not currently come with tools that enhance or manage the RScript and R CMD BATCH functions. However, there is a shell built into the IDE and you could conceivably call these commands from there.

 The alternative to the using the Linux command line is to use the source() function inside of R. The source function will also call a script, but you have to be inside an R session to use it.

References:

How to run R scripts from the command line (Nathan Stephens on January 02, 2017)

Running R batch mode on Linux (pdf)

RScript man page

Setup R environment on Ubuntu 16.04 (R-Base and RStudio)

This post provides instructions for installing R-Base and RStudio on Ubuntu 16.04.

  • Install R-Base

You can find R-Base in the Software Center; this would be the easy way to do it. However, the Software Center versions are often out of date, which can be a pain moving foward when your packages are based on the most current version of R Base. The easy fix is to download and install R Base directly from the Cran servers.

1. Add R repository

First, we’ve got to add a line to our /etc/apt/sources.list file. This can be accomplished with the following. Note the “xenial” in the line, indicating Ubuntu 16.04. If you have a different version, just change that.

sudo echo "deb http://cran.rstudio.com/bin/linux/ubuntu xenial/" | sudo tee -a /etc/apt/sources.list

2. Add R to Ubuntu Keyring

First:

 gpg --keyserver keyserver.ubuntu.com --recv-key E084DAB9

Then:

 gpg -a --export E084DAB9 | sudo apt-key add -

3. Install R-Base

sudo apt-get update
sudo apt-get install r-base r-base-dev

 

If you would like to use R in IDE like RStudio, See below for the instructions.

  • Installing RStudio

Use CTRL + ALT + T to open your terminal, then use the commands below. If you would like to install the latest version, just change the link info after the wget command. (Note that you can get latest RStudio download link at here. See the picture below the install commands to see how to get the latest version of RStudio for you. Be sure to revise the command part associated with the version you would like to install accordingly, which I highlight in red and italic below.)

# Download and Install RStudio
sudo apt-get install gdebi-core
wget https://download1.rstudio.org/rstudio-1.0.136-amd64.deb
sudo gdebi rstudio-1.0.136-amd64.deb
rm rstudio-1.0.136-amd64.deb

References:

How to Install R on Linux Ubuntu 16.04 Xenial Xerus (April 26, 2016 By Kris Eberwein)

Install R and RStudio on Ubuntu 12.04/14.04/16.04 (Michael Galarnyk on Dec 17, 2016 )