[R] how to avoid infinite loops when sourcing R files

From: Adrian Dragulescu <adrian_d_at_eskimo.com>
Date: Wed, 09 May 2007 14:58:08 -0700 (PDT)

Hello,

I have a bunch of R files in a directory and I want to source all of them with something like lapply(files, source).

I have a main.R file
source("C:/Temp/set.parms.R")
parms <- set.parms()
do.calcs(parms)
cat("I'm done with main.R\n")

Then I have set.parms.R function
set.parms <- function(){
  cat("I'm in set.parms.\n"); flush.console()   directory <- "C:/Temp/"
  files <- dir(directory, "\\.[rR]$", full.name=T)   files <- files[-grep("set.parms.R", files)] # remove infinite loop   lapply(files, source) # source them all

  cat("Exiting set.parms.\n"); flush.console()
}

And other functions f1, f2, f3, etc. in the same directory that also source set.parms.R. For example:
f1 <- function(){
  source("H:/user/R/RMG/Energy/VaR/Overnight/Test/set.parms.R")   cat("I add two numbers.\n"); flush.console()
}

Because of the source command in f1, I get into an infinite loop. This must be a common situation but I don't know how to avoid it. I need the source(set.parms) in f1, f2, f3, etc. because I want use a different combination of them in other projects.

Thanks,
Adrian



R-help_at_stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 May 2007 - 22:12:02 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 May 2007 - 22:31:22 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.