Levasco
Levasco

Reputation: 169

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)

Upvotes: 0

Views: 2003

Answers (2)

mel
mel

Reputation: 11

The accepted answer really helped me, thank you!

In poking around the documentation, I also discovered that Rscript also takes multiple expressions, provided each expression is preceded by the "-e" flag.

So, this would also work:

Rscript -e "source('file1.R')" -e "source('file2.R')" [args]

Upvotes: 0

Hong Ooi
Hong Ooi

Reputation: 57686

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")'

Upvotes: 1

Related Questions