Why (Or When) Is Rscript (Or Littler) Better Than R Cmd Batch

Why (or when) is Rscript (or littler) better than R CMD BATCH?

R CMD BATCH is all we had years ago. It makes i/o very hard and leaves files behind.

Things got better, first with littler and then too with Rscript. Both can be used for 'shebang' lines such as

 #!/usr/bin/r

#!/usr/bin/Rscript

and both can be used with packages like getopt and optparse --- allowing you to write proper R scripts that can act as commands. If have dozens of them, starting with simple ones like this which I can call as install.r pkga pkgb pkgc and which will install all three and their dependencies) for me from the command-line without hogging the R prompt:

#!/usr/bin/env r       
#
# a simple example to install one or more packages

if (is.null(argv) | length(argv)<1) {
cat("Usage: installr.r pkg1 [pkg2 pkg3 ...]\n")
q()
}

## adjust as necessary, see help('download.packages')
repos <- "http://cran.rstudio.com"

## this makes sense on Debian where no packages touch /usr/local
lib.loc <- "/usr/local/lib/R/site-library"

install.packages(argv, lib.loc, repos)

And just like Karl, I have cronjobs calling similar R scripts.

Edit on 2015-11-04: As of last week, littler is now also on CRAN.

Different results from Rscript and R CMD BATCH

It is possible that one of them is loading up a previously saved workspace and using global variables. Have you checked whether it matters which directory you are in or if there are any .Rhistory files present? One way to ensure that you don't have any hidden variables is to clear the worspace at the beginning of each script. For example, rm(list=ls()) as the first line of your Rscript.

Also, you can pipe output to a file with an Rscript using sink().

R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?

Caveat: I work much more on Linux than Windows:

  • Rcmd.exe is a historical left-over as back in the day, you could not do R CMD something on Windows but needed the special executable Rcmd.exe something. That is no longer the case, yet it is provided for backwards compatibility.
  • Rterm.exe is also a holdover from the days when Rcmd.exe was used. Can be ignored these days.
  • R CMD BATCH is a crutch that was needed in the days before littler and Rscript.exe, and similarly lingering from old docs and habits..
  • Rscript.exe is your friend for batch scripts; use it.
  • For everything else, there's R.exe.

Other than that, as Marek hinted, the reference manual is the wrong one among the six available manuals. Try the Introduction to R and the Installation and Admin manuals both of which have specific appendices for Windows.

R phangorn Rscript R CMD BATCH different results

This works with Rscript if you import library(methods) in addition to phangorn. This changes the sessionInfo() for methods from "loaded via a namespace (and not attached)" to "attached base packages", which was the only difference between your sessionInfo() for Rscript vs R CMD BATCH.

I don't know why the two would differ. However, this answer remarks that littler (an alternative to Rscript) "load the methods package", which would suggest that the authors of littler have experienced your issue too.

External graphical device for littler or Rscript

Besides John's suggestion, you could be explicit and invoke one of the GUI packages to bring up a new 'frame' (or 'window') that then shows the plot. That will remain on-screen until the user (or an external event) terminates that window.

The tcltk package can be used along with the tkrplot package; this is the most portable. RGtk2 is more modern but harder to install / use on Windows as Gtk2 is not exactly native there. There is more---search for R GUIs here and on other places on the intertubes.

Passing Parameters to an R Script via CMD or Batch

You can do all of this in R using one of these packages to parse command-line options:

  • docopt (my favourite)
  • optparse
  • argparse
  • getopt

or doing it manually -- not recommended.

You also do not want the older R CMD BATCH -- use Rscript (or littler, but littler does not work on Windows).

Code Example

#!/usr/bin/Rscript

suppressMessages(library(docopt))

doc <- "Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]

-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text"

opt <- docopt(doc) # docopt parsing

print(opt)

Use with -h

You get a nice message, automatically, with not formatting need:

edd@rob:/tmp$ Rscript so50256138.R -h
Usage: foo.R [-h] [-x] [--src REPODIR] [--out OUTDIR] [FILES...]

-s --src REPODIR source root directory [default: ~/git]
-o --out OUTDIR output directory [default: /tmp]
-h --help show this help text
edd@rob:/tmp$

Use with argument

Note how one default argument is used, and the other from the command-line:

edd@rob:/tmp$ Rscript so50256138.R -s A 
List of 9
$ --src : chr "A"
$ --out : chr "/tmp"
$ --help: logi FALSE
$ -x : logi FALSE
$ FILES : list()
$ src : chr "A"
$ out : chr "/tmp"
$ help : logi FALSE
$ x : logi FALSE
NULL

You can access them in opt by name or by option flag.

The docopt site has more; this is actually a portable specification and the CRAN package implements it for R.

Why does running shiny start two Rscript processes on windows when launching from cmd line?

This is a general thing with the R scripting command-line utilities (Rscript.exe, R.exe, Rcmd.exe) and not specific to Shiny. All of these actually call Rterm.exe underneath to run the actual program in a separate process.

Try Rscript.exe with --verbose and you'll see what I mean:

> Rscript --verbose script.R
running
'C:\PROGRA~1\R\R-34~1.0\bin\x64\Rterm.exe --slave --no-restore --file=script.R'

You can try out the other tools and see how they behave.

For more info, check out the source code

  • https://github.com/wch/r-source/blob/trunk/src/gnuwin32/front-ends
  • https://github.com/wch/r-source/blob/trunk/src/unix/Rscript.c

This question has some good info as well

  • R.exe, Rcmd.exe, Rscript.exe and Rterm.exe: what's the difference?


Related Topics



Leave a reply



Submit