4

I am developing a package that exposes an R interface (a bunch of functions to be used interactively) and a command line interface via Rscript. This second one works via a small launcher, for instance, at the command line:

Rscript mylauncher.R arg1 arg2 arg3

would call a function of my package. I would like to test a couple of command lines from R. Nothing fancy, just make sure that everything runs without errors. If I test these calls doing in an R source file:

system("Rscript mylauncher.R arg1 arg2 arg3")

How can I be sure that I called the right Rscript? In case there are multiple R installations? (which is actually the case in my setting). Another approach would be write in the R source file:

source("mylauncher.R")

But I don't see how to specify the command line (I would avoid the trick of overwriting the function commandArgs, because I want to test also the right tokenization of the command line). Does anybody have an idea?

Thanks!

figurine
  • 746
  • 9
  • 22
JavaNewbie
  • 241
  • 3
  • 6
  • Are you aware that "you can call R from R"? In other words, you have not explained why you need to out to `Rscript` when you already are in R. – Dirk Eddelbuettel Apr 24 '15 at 16:36
  • @DirkEddelbuettel I believe that I explained it "I would like to test a couple of command lines from R", i.e. I want to test if some commands written in a unix terminal lead to an error, and this error is not due to the wrong version of Rscript being called. – JavaNewbie Apr 24 '15 at 16:59
  • maybe (on a Unix system) `system("which Rscript")` ? – Ben Bolker Apr 24 '15 at 17:47

1 Answers1

5

Regarding

How can I be sure that I called the right Rscript? In case there are multiple R installations?

you would query R RHOME on the command-line and Sys.getenv("R_HOME") from wihthin R.

You then append bin/RScript and should have the Rscript corresponding to your current session. I still design my libraries in such a way that I can call them from R ...

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725