Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run multiple R Scripts in R Studio

Tags:

r

rstudio

I am having a lot of R Scripts created by RStudio, and I am wondering if there is a method to run all of them in RStudio with a single step instead of open and run each of them one by one.I know that I can copy and paste them together into one same script, but it will make it too large and it's also a time consuming step. Thank you!

like image 417
Samuel Song Avatar asked Oct 29 '13 17:10

Samuel Song


People also ask

Can you run multiple R scripts at once?

RStudio Workbench (previously RStudio Server Pro) gives you the ability to open multiple concurrent sessions. Multiple concurrent sessions can be useful when you want to: Run multiple analyses in parallel.

How do you run all R codes at once?

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

How do I run a Rscript in RStudio?

You can open an R script in RStudio by going to File > New File > R script in the menu bar. RStudio will then open a fresh script above your console pane, as shown in Figure 1-7. I strongly encourage you to write and edit all of your R code in a script before you run it in the console.


1 Answers

You could have one main script that sources the others and just run the main script.

main.R

print("Hello main")
source("blah.R")
source("foo.R")

blah.R

print("Hello blah")

foo.R

print("Hello foo")

Run them all by sourcing main.R

> source("main.R")
[1] "Hello main"
[1] "Hello blah"
[1] "Hello foo"
> ?source

source {base}

source causes R to accept its input from the named file or URL or connection. Input is read and parsed from that file until the end of the file is reached, then the parsed expressions are evaluated sequentially in the chosen environment.

like image 147
bnjmn Avatar answered Sep 29 '22 09:09

bnjmn