1

I am trying to source() several R scripts using a for loop, as such:

script_list <- list("script1.R", "script2.R", "script3.R")

for (i in 1:length(script_list)){
  try(source(script_list[[i]]))
}

However, I often find that some of the scripts have not been sourced. I therefore have two related questions: a) what errors in earlier scripts could prevent execution of later scripts? b) what function/code would allow for all of the scripts to be executed, regardless of errors in earlier scripts?

histelheim
  • 4,938
  • 6
  • 33
  • 63
  • Can you make this example [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by giving specific (minimal) examples of script files such that you don't get the behavior you expect? – MrFlick Oct 23 '15 at 04:25
  • Try removing the `try()` and your loop will halt if one of the scripts contains an error. Also you can use the simpler `script_list <- c("script1.R", ...)` and then iterate over that vector with `for (f in script_list)`. – Ken Benoit Oct 23 '15 at 04:55
  • How about lapply? Like this: `lapply(script_list, function(x) try(source(x)))` – Antti Oct 23 '15 at 05:26
  • @Antti: At the Hadley Wickham Master R workshop I was told that `lapply()` doesn't necessarily execute the functions in the order specified, and therefore a for loop would be better. – histelheim Oct 23 '15 at 11:25
  • @histelheim: I didn't know that. I need to be careful in the future then. – Antti Oct 24 '15 at 07:34

0 Answers0