Reputation: 561
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
Upvotes: 46
Views: 82688
Reputation: 10223
I think you're simply looking for the source
function. See ?source
. I often have a master script which source
other .R
files. E.g:
source("path/load_data.R")
source("path/analysis.R")
Upvotes: 43
Reputation: 812
You can source all .R
scripts from a folder:
# Load tidyverse
library(tidyverse) # to pipe (%>%) and map across each file
# List files and source each
list.files("path_to_folder", full.names = TRUE) %>% map(source)
Here, you list all files from a folder, then you map across the source()
function.
This solution is more useful if each script contains some functions, and you would then like to use these functions in a master_script
.
Upvotes: 8
Reputation: 6210
I've done what you described and split up chunks of code in separate R files and have been running source(this) and source(that), but I've been painfully learning that sourcing functions (rather than subroutines/script files) is the better way to go.
Here are 3 possible reasons why we might have developed their scripts in this way and stuck to it, and 3 reasons why switching to functions makes sense:
2a) We didn't know what variables needed to be kept for later (didn't want to keep track of which variables to put into functions and which variables to output from functions)
2b) We have a large number of variables for everything (parameters/variables, settings, different parts of data) so it's impractical to stuff everything in and out of functions.
Related SO Q&A:
Comments from others welcome!
Upvotes: 10
Reputation: 776
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")
}
Upvotes: 14
Reputation: 65
Although I understand your need for modularity, why not simply create a single script for the run of interest. Sourcing multiple scripts results in complexities of not being able to pass variables across scripts unless you write to files (which wastes CPU cycles). You could even build a master script that would read the text contents of each script and then create the master script and then run that script.
Upvotes: 1