将多个.rda文件加载到R中的列表中

时间:2018-07-24 04:59:22

标签: r save purrr rda

我运行了各种模型(glm,rpart,earth等),并将模型对象从每个模型对象导出到计算机上的文件夹中。因此,我现在有一个文件夹,其中存储着约60种不同型号的.rda文件。

这是通过创建模型函数,然后通过purrr映射包将其应用于模型类型列表来完成的(以避免错误和终止)。

我现在想将它们重新加载到r中并进行比较。不幸的是,当我编写初始模型脚本时,每个模型都存储为相同的模型,即“ Model.Object”(否则,我不知道如何做),因此当我尝试将每个模型分别加载到r中时,它们会彼此重叠。每个文件都另存为glm.rda,rpart.rda,earth.rda等,但其中的模型标记为Model.Object(为澄清起见)。

所以我想我有几个问题; 1.可以将多个.rda文件加载到r中,然后再进行索引 2.如何更改已应用的模型功能,以使“ model.object”名称读取为模型类型(例如glm,rpart等)

代码:

    Model.Function = function(Model.Type){

  set.seed(0)
  Model.Output = train(x = Pred.Vars.RVC.Data, y = RVC, trControl = Tcontrolparam,
                       preProcess = Preprocessing.Options, tuneLength = 1, metric = "RMSE",
                       method = Model.Type)

    save(Model.Object, file = paste("./RVC Models/",Model.Type,".rda", sep = ""))

  return(Model.Object)

}

Possibly.Model.Function = possibly(Model.Function, otherwise = "something wrong here")

result.possible = map(c("glm","rpart","earth"), Possibly.Model.Function)

1 个答案:

答案 0 :(得分:1)

就目前而言,现有文件的抢救操作可能看起来像这样(在@nicola关于对sumankumar@TL110 MINGW64 ~/Desktop/jenkins $ java -jar jenkins.war Running from: C:\Users\sumankumar\Desktop\jenkins\jenkins.war webroot: $user.home/.jenkins Jul 24, 2018 11:03:32 AM org.eclipse.jetty.util.log.Log initialized INFO: Logging initialized @975ms to org.eclipse.jetty.util.log.JavaUtilLog Jul 24, 2018 11:03:32 AM winstone.Logger logInternal INFO: Beginning extraction from war file Jul 24, 2018 11:03:32 AM winstone.Logger logInternal INFO: Jetty shutdown successfully java.io.FileNotFoundException: C:\Users\sumankumar\.jenkins\war\META-INF\JENKINS.SF (Access is denied) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at winstone.HostConfiguration.getWebRoot(HostConfiguration.java:278) at winstone.HostConfiguration.<init>(HostConfiguration.java:81) at winstone.HostGroup.initHost(HostGroup.java:66) at winstone.HostGroup.<init>(HostGroup.java:45) at winstone.Launcher.<init>(Launcher.java:148) at winstone.Launcher.main(Launcher.java:363) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:344) at Main.main(Main.java:160) Jul 24, 2018 11:03:32 AM winstone.Logger logInternal SEVERE: Container startup failed java.io.FileNotFoundException: C:\Users\sumankumar\.jenkins\war\META-INF\JENKINS.SF (Access is denied) at java.io.FileOutputStream.open0(Native Method) at java.io.FileOutputStream.open(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at java.io.FileOutputStream.<init>(Unknown Source) at winstone.HostConfiguration.getWebRoot(HostConfiguration.java:278) at winstone.HostConfiguration.<init>(HostConfiguration.java:81) at winstone.HostGroup.initHost(HostGroup.java:66) at winstone.HostGroup.<init>(HostGroup.java:45) at winstone.Launcher.<init>(Launcher.java:148) at winstone.Launcher.main(Launcher.java:363) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at Main._main(Main.java:344) at Main.main(Main.java:160) 使用envir参数的评论之后):

load()

展望未来,使用rda2list <- function(file) { e <- new.env() load(file, envir = e) as.list(e) } folder <- "./RVC Models" files <- list.files(folder, pattern = ".rda$") models <- Map(rda2list, file.path(folder, files)) names(models) <- tools::file_path_sans_ext(files) 将模型另存为.Rds文件比使用saveRDS()更容易。这样,在加载文件时就很容易进行重新分配。参见例如this question and answer,以获取有关此问题的更多详细信息。