I have a collection of .RData files that have result objects that I would like to combine. Specifically, skatCohort objects from the skatMeta package, but I've run into a similar issue with simple data.frames also. If I run something like FILES <- list.files(path="/path/to/my/results", pattern=".RData$", full.names=TRUE) combined <- NULL for (FILE in FILES) { load(FILE) if (!is.null(combined)) { combined <- c(combined, res) } else { combined <- res } } I get all my objects combined. However, if I wrap this into a function I get the following error c_objects <- function(FILES) { combined <- NULL for (FILE in FILES) { load(FILE) if (!is.null(combined)) { combined <- c(combined, res) } else { combined <- res } } return(combined) } combined_results <- c_objects(FILES) Error in eval(expr, envir, enclos) : object 'combined' not found How should I write this function such that it can find "combined". I've tried reading the help on envirnaments, and the exisits function but I haven't been able to figure this out. Are there any other resources to read up on this? Thanks in advance, -Brian
I probably should have been more specific. The .RData objects which get loaded have a list called 'res'. This isn't a reproducible example, as I haven't included any of the RData files. -----Original Message----- From: Rui Barradas [mailto:ruipbarradas at sapo.pt] Sent: Monday, June 17, 2013 4:25 PM To: Davis, Brian Subject: Re: [R] Help understanding environments Hello, The error I get is: Error: object 'res' not found You are using an undefined variable, 'res'. I don't believe this has something to do with environments. Rui Barradas Em 17-06-2013 22:02, Davis, Brian escreveu:> I have a collection of .RData files that have result objects that I would like to combine. Specifically, skatCohort objects from the skatMeta package, but I've run into a similar issue with simple data.frames also. > > If I run something like > > FILES <- list.files(path="/path/to/my/results", pattern=".RData$", > full.names=TRUE) combined <- NULL > for (FILE in FILES) { > load(FILE) > if (!is.null(combined)) { > combined <- c(combined, res) > } else { > combined <- res > } > } > > I get all my objects combined. However, if I wrap this into a > function I get the following error > > c_objects <- function(FILES) { > combined <- NULL > for (FILE in FILES) { > load(FILE) > if (!is.null(combined)) { > combined <- c(combined, res) > } else { > combined <- res > } > } > return(combined) > } > > combined_results <- c_objects(FILES) > Error in eval(expr, envir, enclos) : object 'combined' not found > > How should I write this function such that it can find "combined". I've tried reading the help on envirnaments, and the exisits function but I haven't been able to figure this out. Are there any other resources to read up on this? > > Thanks in advance, > > -Brian > > ______________________________________________ > R-help at r-project.org 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. >
On 13-06-17 5:02 PM, Davis, Brian wrote:> I have a collection of .RData files that have result objects that I would like to combine. Specifically, skatCohort objects from the skatMeta package, but I've run into a similar issue with simple data.frames also. > > If I run something like > > FILES <- list.files(path="/path/to/my/results", pattern=".RData$", full.names=TRUE) > combined <- NULL > for (FILE in FILES) { > load(FILE) > if (!is.null(combined)) { > combined <- c(combined, res) > } else { > combined <- res > } > } > > I get all my objects combined. However, if I wrap this into a function I get the following error > > c_objects <- function(FILES) { > combined <- NULL > for (FILE in FILES) { > load(FILE) > if (!is.null(combined)) { > combined <- c(combined, res) > } else { > combined <- res > } > } > return(combined) > } > > combined_results <- c_objects(FILES) > Error in eval(expr, envir, enclos) : object 'combined' not found > > How should I write this function such that it can find "combined". I've tried reading the help on envirnaments, and the exisits function but I haven't been able to figure this out. Are there any other resources to read up on this?You are doing something that you aren't showing us: I don't see any calls to eval(), but it's eval() that generated the error. Calling traceback() after the error might be informative. Duncan Murdoch