Using purrr to load multiple rda files

389 Views Asked by At

I have a folder with several rda files, and want to load all of them. I have tried with:

file_list<-list.files("~/my_data/", full.names = TRUE)
walk(file_list, load)

and I get nothing.

I've also tried with map and lapply, and I get a list with the names of the objects in each rda file. What I'm doing wrong?

2

There are 2 best solutions below

0
PaulS On

We need to load the rda files to the global environment (by default, inside walk, the rda files are read to the parent environment):

file_list <- list.files("~/my_data/", full.names = TRUE)
walk(file_list, ~ load(.x, .GlobalEnv))
0
Jean-Alexandre Patteet On

Have you tried something like this. It will create a list with all the files you have in the selected folder. Then add to the file name the path so that the load function can access the file. load them all thanks to a loop

path<- "mydata/"#where your data is

file_list <- list.files(path) #list of file names
file_list <- paste0(path,file_list) #adding to the file name, the path to get it

#loading files
for (i in 1:length(file_list)){
  load(file_list[i])
}