Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine and run multiple R scripts from another script

Tags:

r

organization

Essentially, I have constructed a sizable predictive model in R with about 10~15 separate script files for collecting, sorting, analyzing and presenting my data. Rather than just put everything into one gigantic script file, I would like to maintain some level of modularity and run each piece from a control script, or some kind of comparable control mechanism, as I've done in matlab before. Is this possible in R?

I have read this thread as well as its related threads, but couldn't find this exact answer. Organizing R Source Code

like image 764
Christian Avatar asked Aug 12 '14 20:08

Christian


People also ask

How do I run an Rscript from another Rscript?

You can execute R script as you would normally do by using the Windows command line. If your R version is different, then change the path to Rscript.exe. Use double quotes if the file path contains space.

Can I run multiple R scripts at once?

You can open both simultaneously, which will result in two rsessions. You can then open each script in each project, and execute each one separately. It is then on your OS to manage the core allocation.

How do I run an Rscript all at once?

To run the entire document press the Ctrl+Shift+Enter key (or use the Source toolbar button).

How do I run an Rscript from a shell script?

Use Rscript to run R from bash R comes with Rscript , a command line tool that can run R scripts or R commands directly from the bash shell. You can run it using Rscript test. r . And even better, if you add an initial shebang line #!/usr/bin/env Rscript in the script above and make it executable with chmod +x test.


2 Answers

I think you're simply looking for the source function. See ?source. I often have a master script which source other .R files.

like image 182
Anders Ellern Bilgrau Avatar answered Sep 30 '22 13:09

Anders Ellern Bilgrau


I am a new developer and I am answering with an example that worked for me because no one has given an example. Example of using source("myscript.R"), to call another R script "myscript_A.R" or "myscript_B.R" is as follows-

if(condition==X){     source("myscript_A.R") }else{     source("myscript_B.R") } 
like image 43
Ajay B Avatar answered Sep 30 '22 14:09

Ajay B