I want to run my main R script multiple times to get an interval (the output of this script is an avgerage (numeric number)). However, within this main R script, I access others sub-R scripts with source() by following this (Run multiple R Scripts in R Studio).
Here's the problem: I use set.seed() in every sub-R scripts to make sure I get the same number across all sub-R scripts. And now I want to run set.seed(1) to set.seed(10). How can I do this?
Desired output (just the idea):
main R script:
for (i in 1:10){
source("\...\sub_R_scrip1.R")
source("\...\sub_R_scrip2.R")
...
source("\...\sub_R_scrip10.R")
}
sub_R_scripX:
set.seed(1) # when i = 1 in the loop of the main R script
train.row = sample(nrow(df), 100)
train.data = df[train.row,]
test.data = df[-train.row,]
xtrain = train.data[,1:49]
ytrain = train.data[,50]
xtest = test.data[,1:49]
ytest = test.data[,50]
How can i approach this?
functions instead ofsourceing your code? It's much easier in the long run.if (!exists("args")) args <- list(...) ; if ("seed" %in% names(args)) set.seed(args$seed), where your main script can setargsto be the "arguments" used to initiate each source file. It's a hack.