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

______________________________________________
[email protected] 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.

Reply via email to