Running GNU R on Linux Operating System

GNU R can be run on the Linux operating system in a number of ways. In this article we will describe running R from the command line, in an application window, in a batch mode and from a bash script.

You will see that these various options for running R in Linux will suit a specific task. Some of them are more suitable for simple statistical analysis that can be done in one line of code, others for more sophisticated programs that require executions of a larger number of R expressions.

Finally, we may want to run a program that will take a day or two to run on a Linux cluster. In this case we will run R in a background, which allows us for logging out from the cluster.

In this tutorial you will learn:

  • How to install R on major Linux distros
  • How to run R from the Linux command line
  • How to run R in an application window
  • How to run R in batch mode
  • How to run R from a Linux Bash script
How to run R in linux
How to run R in linux
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux system
Software GNU R
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install R on major Linux distros




You can use the appropriate command below to install GNU R with your system’s package manager.

To install GNU R on Ubuntu, Debian, and Linux Mint:

$ sudo apt install r-cran-littler

To install GNU R on Fedora, CentOS, AlmaLinux, and Red Hat:

$ sudo dnf install R

To install GNU R on Arch Linux and Manjaro:

$ sudo pacman -S r

Running R from the Linux command line

Probably, the simplest way to run R under Linux is to run it from the Linux command line. Simply like this:

$ R

As a result of this command, an R prompt on which you can enter more commands will appear. Here is what it looks like:

Running R from the Linux command line
Running R from the Linux command line

The above shows the version of R installed on your Linux platform. It also provides a few commands, which are built in to assist you with help. We will not elaborate further on these help command as we will deal with them in later articles. At the moment, we would like to point out that in order to exit R we simply type:

> q()

This will give us additional option to save the workspace image:

Save workspace image? [y/n/c]: 

This way of launching R is more suitable for shorter statistical analysis, which do not involve execution of a large number of R commands.

Running R in an application window under Linux

An application widow for R under Linux is similar to a graphical user interface used on other platforms. Type the following to launch R in an application window:

$ R -g Tk &

The figure below shows such window under Linux.

Launching R in an application window under Linux
Launching R in an application window under Linux




The menu in the tk-R application window gives you a few options. These are, installing and loading packages and sourcing code from files. It includes also some demo examples as well as access to R help. Basically, what we can see in the figure above is the R console.

The application window lacks a toolbar as seen on other platforms. However, the R console itself allows you to type expressions (commands), which are then interpreted by the R system and the response is output on the screen. The application window is similar to running R in a Linux command line.

Similarly as before, we would not run a large number of commands in the application widow. The following two ways of running R allow us for such more complex implementations.

Running R in a batch mode in Linux

Running R in a batch mode in Linux provides a way to execute a large set of commands in sequence and save the results to a file. Let us now create our first function in R and save it in a file called r-example-function-1.R. This function will load some data from a file then apply an exponential function to the data and save the output in the corresponding file.

  1. In the first step produce the r-example-function-1.R file including:
    r_example_function_1<-function()
    {
    data<-read.csv("gnu-r-example.csv",header=F)
    expdata<-exp(data[,1])
    write.csv(expdata,"output_gnu-r-example.csv")
    }
    r_example_function_1()
    

    Download also gnu-r-example.csv to your working directory.

  2. You can now run the commands included in the file r-example-function-1.R in a bash mode as follows:
    $ R CMD BATCH r-example-function-1.R
    

    This will produce an output file called r-example-function-1.Rout and the file output_gnu-r-example.csv which was produced by the function r_example_function_1() defined in the r-example-function-1.R file.

  3. For more information about running R from the Linux command line including available options, type:
    $ R --help
    


Running R from a bash script in Linux

Running R from a bash script in Linux involves writing a bash script including R functions and then calls to these functions. For instance, create a file called r-bash-example.sh as indicated below.

  1. First, create the r-bash-example.sh with the following content:
    #!/bin/bash
    
    R --no-save <
    
  2. Now, make this file executable by
    $ chmod +x r-bash-example.sh
    
  3. To run the R function included in this file type:
    $ ./r-bash-example.sh 
    
  4. Note that it is also possible to execute such script in the background on, for instance, a Linux cluster by:
    $ nohup ./r-bash-example.sh &
    

    This allows you to log out the cluster and leave the program running. Note, that this will, additionally, produce an R output file: nohup.out.

Closing Thoughts

This article only scratches the surface of the possibilities of running R. For example, R can be integrated with other programming languages such as Java. Moreover, analysis obtained by R software can be integrated into a web application.

GNU R tutorial series

Part I: GNU R Introductory Tutorials:



  1. Introduction to GNU R on Linux Operating System
  2. Running GNU R on Linux Operating System
  3. A quick GNU R tutorial to basic operations, functions and data structures
  4. A quick GNU R tutorial to statistical models and graphics
  5. How to install and use packages in GNU R
  6. Building basic packages in GNU R

Part II: GNU R Language:

  1. An overview of GNU R programming language


Comments and Discussions
Linux Forum