This might be a very basic question, but have not found a convincing answer yet. When executing an R script within the R environment, I can either do source(foo.R) or system("Rscript foo.R"). Is there any difference at all between the two approaches, and if so how do the two approaches compare?
This might be a very basic question, but have not found a convincing answer
Share
They’re fundamentally different in their effects.
source("foo.R")tells your current R process to take its input from"foo.R".system("Rscript foo.R")uses an operating system command to launch a separate R process, within which the contents of"foo.R"are evaluated.The
Rscriptcall won’t directly affect your current R session at all, except that it will by default print the output of that other R session on your current console. (You can disable this in yoursystem()call by settingshow.output.on.console=FALSE).