简体   繁体   中英

Loading multiple workspaces into R at once

Consider having many *.Rda files in your directory. They all contain exactly one object (in this case, a model obtained from mboost:::gamboost) with the added twist, that the objects have the same name ("mod_gam").

Is it possible to load all of them into workspace at once (and even renaming them)?

temp <- list.files(pattern="*.Rda")
models <- lapply(temp, load)

does yield a list with empty characters:

str(models)
List of 26
 $ : chr "mod_gam"
 $ : chr "mod_gam"
 $ : chr "mod_gam"

... and so on.

My suggestion would be to add an iterative suffix to your objects as they are loaded in. Since you already know that every object loaded in will be called "mod_gam", it makes things a bit easier.

i <- 1
for(each in temp){
    load(each)
    eval(parse(text=paste(paste0("mod_gam_",i),"<- mod_gam")))
    i <- i+1
}

This will give you the 26 different objects. Note that this isn't optimal -- I wanted to lapply instead of loop, but I was having trouble figuring out how to iterate the suffix each time I read in a new file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM