[R] Calling r-scripts with arguments from other scripts, or possibly R programming conventions/style guide

MacQueen, Don m@cqueen1 @end|ng |rom ||n|@gov
Wed Jun 13 22:21:07 CEST 2018

When I want to run multiple scripts one after the other, and have variables created in a script be still in memory for use by a subsequent script, I normally create a master script (say, "runall.r") and it sources each of the others in turn. For example, my master script (runall.r) would look like this:

## master script (runall.r)
cat('[runall.r] Done\n')
## end of master script

Then, optionally, I can pass command line arguments to runall.r and parse them before sourcing the other scripts.

Since all three scripts are run in the same R session, all variables in memory when script1 finishes will be available to script2, and so on.

If you want to have each of the scripts be executable (by which I mean you run them by typing their name at the command line in a Linux environment; I don't know how it's done in Windows) and have the results of script1 be used as command line arguments to script2 -- well, I'm sure it can be done, but I don't think R's way of doing things is conducive to this approach. If you want to go that route, look up the --save, --no-save, --restore, and --no-restore arguments to R scripts.

See also the help page for Rscript [type help('Rscript') or ?Rscript at the R prompt] if you haven't already.

In my opinion, there are pros and cons to William Dunlap's suggestion to embed everything in functions in a package. I do it either way, depending on various factors. I would suggest, however, that you wait until you have more R experience before trying that route.

Incidentally, this construct:

  writeLines(c(TotalDeviation, IndividualDeviation, RecenterPeriods), fileConn)

looks to me like it's more complex than needed.
I would suggest

  cat( TotalDeviation, IndividualDeviation, RecenterPeriods, '\n', file='output.txt')

Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
Lab cell 925-724-7509

On 6/12/18, 7:33 PM, "R-help on behalf of Sam Tuck" <r-help-bounces using r-project.org on behalf of STuck using nzsuperfund.co.nz> wrote:

    Hi All,
              I am new to R and am wondering if there is a way to pass arguments between rscripts.  I have this working but have had to create a C# shell calling the scripts in sequence via windows scripting which enables command line arguments to get the necessary interaction.  
    I'm wondering if I'm using an outdated program construction technique - I create r files like I would programme functions or reoccurring code snippets in C.  It may be that r was not designed to create lots of little r script modules that interact via a master script? 
    Ideally I'd like to call r scripts from other r scripts and have all the variables still in memory: For example
    I've been using RStudio Version 1.1.447 to programme and regression test my individual scripts,. 
    Script Arg Script.R
    # We are going to pass arguments into this script
    arguments <- commandArgs(trailingOnly = TRUE)
    #arguments[1] is double
    #arguments[2] is double
    #arguments[3] is double.
    if(length(arguments) <3) 
      stop("Not enough arguments, please supply 3, [% dbl}total deviation, [% dbl] individual deviation, [int] periods before recenter")
    TotalDeviation <- as.numeric(arguments[1])/100
    IndividualDeviation <- as.numeric(arguments[2])/100
    RecenterPeriods <- as.numeric(arguments[3])
    # We then manipulate some objects based on these inputs, but for this test we will output them to a file. 
    writeLines(c(TotalDeviation, IndividualDeviation, RecenterPeriods), fileConn)
    Script RunningScript.R
    Arg Script.R 0.6 0.4 132
    To which I get
    Error: unexpected symbol in " Arg Script.R"
    When I use the script RunningScript.R
    system(paste("Arg Script.R", 0.8, 0.4, 132))
    Nothing occurs (there is no output file created, but also no error)
    When I use RunningScript.R
    commandArgs <- c(0.6,0.4,132)
    source("Arg Script.R')
    I don't get any args passed into the file.  Instead getting the error 
    Not enough arguments, please supply 3, [% dbl}total deviation, [% dbl] individual deviation, [int] periods before recenter
    Sam Tuck 
    R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
    PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
    and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list