## Introduction

On Linstat, you can use R to process an entire script at once or you can use R interactively. Processing an entire R script at once begins at a linux prompt. To use R interactively log in to RStudio Server from a web browser.

To process lengthy, large, computationally intensive jobs, batch processing is generally preferred.

## Batch or Script Processing

There are three commands you can use to process R scripts from a linux prompt.

(The > is the linux prompt. Items in square brackets, [ ] are optional.)

• Rscript

> Rscript [options] infile [> outfile]

• R

> R {--save|--no-save} [options*] < infile [> outfile]

• R CMD BATCH

> R CMD BATCH [options] infile [outfile]

Any of these alternatives may be put in the background by adding an ampersand, "&", at the end of the command.

Note: All three of these commands automatically restore (load) the default R workspace (.RData). Use the --no-restore option to prevent this.

In addition, Rscript and R CMD BATCH automatically save the R workspace at the end of your script. Use the --no-save option to prevent this.

(The R command requires you to specify --save, --no-save, or --vanilla.)

The following table summarizes the differences among these commands.

Feature Rscript R R CMD BATCH
output goes to terminal or file terminal or file file
errors go to terminal terminal file
commands no echo echo file
R banner none optional file
restore workspace default default default
save workspace default user specified default

All of these commands save graphics in a file named Rplots.pdf by default.

### Rscript

The first command, Rscript infile > outfile, sends only the printed output to the specified output file. Warning and error messages are sent to the terminal. If no output file is specified, output is also sent to the terminal.

Suppose we have a script with the R statements

x <- rnorm(15, mean=1)
t.test(x)

saved in a file names example.r.

At the linstat command prompt we can issue the command

> Rscript --no-save example.r > example.log

Then the file example.log contains these lines


One Sample t-test

data:  x
t = 5.6398, df = 14, p-value = 6.101e-05
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
0.7829532 1.7439090
sample estimates:
mean of x
1.263431


### R

The second command, R --no-save < infile > outfile is similar to Rscript infile > outfile. In addition to the printed output, this command also begins with the R banner messages, and echos each R statement.

To suppress the R banner, we use the --quiet option

> R --no-save --quiet < example.r > example2.log

Then the file example2.log contains these lines

> x <- rnorm(15, mean=1)
> t.test(x)

One Sample t-test

data:  x
t = 2.601, df = 14, p-value = 0.02094
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
0.09341475 0.97177368
sample estimates:
mean of x
0.5325942

>  

### R CMD BATCH

The third command, R CMD BATCH infile, saves the R banner, the infile commands, the printed results they produce, and any warning or error messages, all interleaved in a single file.

Automatic capture of error messages is perhaps the chief advantage of this command. It's biggest drawback is that it lacks a --quiet option.

Issue the command

> R CMD BATCH --no-save example.r

Then the default output file, example.r.Rout, contains these lines


R version 4.1.0 (2021-05-18) -- "Camp Pontanezen"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (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.
'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.

> x <- rnorm(15, mean=1)
> t.test(x)

One Sample t-test

data:  x
t = 4.9306, df = 14, p-value = 0.0002213
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
0.6738831 1.7115150
sample estimates:
mean of x
1.192699

>
> proc.time()
user  system elapsed
0.20    0.14    0.36 
unlink("example.r")
unlink("example.r.Rout")