rbashshellrscript

Running multiple R scripts sequentially from shell in the same R session


Is it possible to run multiple .R files from the shell or a bash script, in sequence, in the same R session (so without having to write intermediate results to disk)?

E.g. if file1.R contains a=1 and file2.R print(a+1)

then do something like

$ Rscript file1.R file2.R
[1] 2

(of course a workaround would be to stitch the scripts together or have a master script sourcing 1 and 2)


Solution

  • You could write a wrapper script that calls each script in turn:

    source("file1.R")
    source("file2.R")
    

    Call this source_files.R and then run Rscript source_files.R. Of course, with something this simple you can also just pass the statements on the command line:

    Rscript -e 'source("file1.R"); source("file2.R")'