Running R in Batch Mode on Linux: Output Issues

Running R in Batch Mode on Linux: Output Issues

nohup runs a command in the background, makes it ignore signals telling it to stop (e.g., when you log off), and redirects the output to a file.
But it has to be an executable command: you probably have error messages in nohup.out telling you that BatchProgram.R could not be run.

The following should work:

nohup Rscript ./BatchProgram.R &

Unix Submission of R Script Batch Mode

the command R --vanilla < Maha.R still looks for R on the default path, the fact that you set an environment variable R=/global/software/R-3.1.0-rh6/bin/R doesn't change that.

To use that variable, you need to change the command to

$R --vanilla < Maha.R

Run R script from command line

If you want the output to print to the terminal it is best to use Rscript

Rscript a.R

Note that when using R CMD BATCH a.R that instead of redirecting output to standard out and displaying on the terminal a new file called a.Rout will be created.

# Check the output
cat a.Rout

One other thing to note about using Rscript is that it doesn't load the methods package by default which can cause confusion. So if you're relying on anything that methods provides you'll want to load it explicitly in your script.

If you really want to use the ./a.R way of calling the script you could add an appropriate #! to the top of the script

#!/usr/bin/env Rscript
sayHello <- function(){


I will also note that if you're running on a *unix system there is the useful littler package which provides easy command line piping to R. It may be necessary to use littler to run shiny apps via a script? Further details can be found in this question.

How can I avoid having my R script printed every time I run it?

Resolution is to run with Rscript, and not with R. Examples elsewhere (e.g. How can I read command line parameters from an R script?), run scripts from the command line with

R --args args1 args2... < foo.R

running with

Rscript foo.R args1 args2 ...

produces only the output, and not the script. It's also a much cleaner way to run scripts.

R Batch mode - Can't execute script

In your example you are trying to look for csv file in the cd c:\Program Files\R\R-3.1.3\bin\x64.

You need to execute R.exe or even better Rscript.exe in the directory where myData.csv is located.

R exe dir is one thing.

R script dir is another.
csv dir is another.

Use absolute paths or...

Considering you have your R script in the same directory as csv file you can use kind of:

CD /path_to_R_script/
path_to_exe/exe script

So you run the exe from the directory where you have csv file.

In linux it could look like:

cd /my/path && Rscript myscript.R

R Session on Linux - submit multiple batch jobs pointing to same session

I'm not sure what your end goal is, but have you considered something like the futures package that will allow R to send work off to another thread to be completed? That way the work can be done but doesn't lock up the main R session while the work is being completed. This way, via the main R session, you could launch job1 and then while that was still being worked on, launch job2.

How to write console output to a text file with Rscript, like you can with R CMD BATCH

I discovered after some digging that, at least on the Linux system I'm using, Rscript is just a convenience function. If you call

Rscript --verbose foobar.r

you will see the underlying call is:

'/usr/lib/R/bin/R --no-echo --no-restore --file=foobar.r'

This means that --no-echo is baked into Rscript.

Therefore the solution is to run

/usr/lib/R/bin/R --no-restore --file=foobar.r > output.txt

where the --no-echo is removed, and the output is redirected to a text file as suggested by @MrFlick. The commands will be echoed in addition to the output.

You can create a new alias for Rscript if you want --no-echo to be removed by default. For example, in my .bashrc file I have the following:

function Rscript2() { R --no-restore --file="$1"; }
export -f Rscript2

Now, in my Slurm batch job scripts, I can run Rscript2 file.R to get the desired behavior: all R console output is included in the slurm-*.out files.

Related Topics

Leave a reply