Loading many files at once?

So let's say I have a directory with a bunch of .rdata files

file_names=as.list(dir(pattern="stock_*"))

[[1]]
[1] "stock_1.rdata"

[[2]]
[1] "stock_2.rdata"

Now, how do I load these files with a single call?

I can always do:

for(i in 1:length(file_names)) load(file_names[[i]]) 

but why can't I do something like do.call(load, file_names)? I suppose none of the apply functions would work because most of them would return lists but nothing should be returned, just that these files need to be loaded. I cannot get the get function to work in this context either. Ideas?


lapply works, but you have to specify that you want the objects loaded to the .GlobalEnv otherwise they're loaded into the temporary evaluation environment created (and destroyed) by lapply.

lapply(file_names,load,.GlobalEnv)

For what it's worth, the above didn't exactly work for me, so I'll post how I adapted that answer:

I have files in folder_with_files/ that are prefixed by prefix_pattern_, are all of type .RData, and are named what I want them to be named in my R environment: ex: if I had saved var_x = 5, I would save it as prefix_pattern_var_x.Data in folder_with_files.

I get the list of the file names, then generate their full path to load them, then gsub out the parts that I don't want: taking it (for object1 as an example) from folder_with_files/prefix_pattern_object1.RData to object1 as the objname to which I will assign the object stored in the RData file.

file_names=as.list(dir(path = 'folder_with_files/', pattern="prefix_pattern_*"))
file_names = lapply(file_names, function(x) paste0('folder_with_files/', x))
out = lapply(file_names,function(x){
  env = new.env()
  nm = load(x, envir = env)[1]
  objname = gsub(pattern = 'folder_with_files/', replacement = '', x = x, fixed = T)
  objname = gsub(pattern = 'prefix_pattern_|.RData', replacement = '', x = objname)
  # print(str(env[[nm]]))
  assign(objname, env[[nm]], envir = .GlobalEnv)
  0 # succeeded
} )

Loading many files in a function?

Here's a modified version of Joshua Ulrich's answer that will work both interactively and if placed within a function, by replacing GlobalEnv with environment():

lapply(file_names, load, environment())

or

foo <- function(file_names) {
  lapply(file_names, load, environment())
  ls()
}

Working example below. It will write files to your current working directory.

invisible(sapply(letters[1:5], function(l) {
  assign(paste0("ex_", l), data.frame(x = rnorm(10)))
  do.call(save, list(paste0("ex_", l), file = paste0("ex_", l, ".rda")))
}))

file_names <- paste0("ex_", letters[1:5], ".rda")
foo(file_names)