0

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?

3
  • 2
    Is there a reason you're not using functions instead of sourceing your code? It's much easier in the long run. Commented Jul 27, 2020 at 20:49
  • @r2evans there will be a lot of things to return if using function instead of source, there are ~200 lines for each sub R script Commented Jul 27, 2020 at 21:00
  • 2
    "lot of things to return" is all relative, but only you can know what is required from each function. I don't know your scripts, your code, so it's always more complicated than it seems. I don't know enough to be able to help much, sorry. I can say this, though: you can have quasi-arguments for a file by starting it with something like if (!exists("args")) args <- list(...) ; if ("seed" %in% names(args)) set.seed(args$seed), where your main script can set args to be the "arguments" used to initiate each source file. It's a hack. Commented Jul 27, 2020 at 21:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.