Share this post
FaceBook  Twitter  
Contents[Hide]

1. Introduction

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. 

2. 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. That is,

$ R

As a result of this command the following appears:

R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows"
Copyright (C) 2012 The R Foundation for Statistical Computing
ISBN 3-900051-07-0
Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

>


The above shows the version of R installed on your Linux platform. It also provides a few commands, which are build 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.

3. 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:

$ R -g Tk &

to launch R in an application widow. The figure below shows such widow 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.

4. 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 fist 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.

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.

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.

For more information about running R from the Linux command line including available options type

$ R --help

5. 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

#!/bin/bash

R --no-save <<EOF
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()

EOF

Now, make this file executable by

$ chmod +x r-bash-example.sh

To run the R function included in this file type

 $ ./ r-bash-example.sh 

Note that it is also possible to execute such script in the background on, for instance, 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.

6. Conclusion

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

And more articles on the way... So subscribe to our RSS for continuous updates.


Partners

Who are we?

LinuxCareer.com is not affiliated with any local or international company, nor is it a recruitment or employment agency. We specialise in Linux based careers and closely related Information Technology fields by providing careers advice and latest employment opportunities.

JOIN LINUXCAREER

You can also get involved in the LinuxCareer project by participating on our FORUM or SUBMITTING A LINUX ARTICLE. We offer a range of privileges to our authors and good company.