如何同时导入大量RData文件

eqqqjvef  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(99)
fs::dir_ls(
  
  path = paste("C:/Users/",Sys.info()["user"],
               "/Dropbox/66_COHORTE_obe/00_RAW_DATA - copie/1_GROUPE BARIA/DIAG/CLEAN DATASET",
               sep = ""),
  glob="*DIAG*.RDS",
  type="file"
)%>%{
  tibble(path=.,
         data=map(., .%>%
                    readRDS()))
}->all_files_DIAG_CLEAN

我使用上面的代码按照文件名模式从我的文件夹中导入了大量RDS文件。
我想尝试同样的事情来导入RData文件

fs::dir_ls(
  
  path = paste("C:/Users/",Sys.info()["user"],
               "/Dropbox/66_COHORTE_obe/00_RAW_DATA - copie/1_GROUPE BARIA/DIAG/CLEAN DATASET",
               sep = ""),
  glob="*DIAG*.RData",
  type="file"
)%>%{
  tibble(path=.,
         data=map(., .%>%
                    load()))
}->all_files_DIAG_CLEAN

我将readRDS()替换为load()。我理解为什么这个方法不起作用,因为你不能把load()赋值给一个对象。
我也试过这个

fs::dir_ls(
  
  path = paste("C:/Users/",Sys.info()["user"],
               "/Dropbox/66_COHORTE_obe/00_RAW_DATA - copie/1_GROUPE BARIA/DIAG/CLEAN DATASET",
               sep = ""),
  glob="*DIAG*.RDS",
  type="file"
)%>%{
  tibble(path=.,
         map(., .%>%
                    load()))
}->all_files_DIAG_CLEAN

也就是说,没有将load()赋值给一个对象,但我仍然无法导入myfiles。
有谁能给点建议吗?

o2g1uqev

o2g1uqev1#

应该可以...但有一些注意事项。您可能需要执行额外的处理,以确保all_files_DIAG_CLEAN中的tibble格式正确。

fs::dir_ls(
  path = paste('C:/Users/', 
               Sys.info()[ 'user' ],
               '/Dropbox/66_COHORTE_obe/00_RAW_DATA - copie/1_GROUPE BARIA/DIAG/CLEAN DATASET',
               sep = ''),
  glob='*DIAG*.RData',
  type='file'
) %>%
  {
    var_names <- lapply(., load) %>% unlist # I assume that there is only 1 variable stored in each .rdata file, this might not be a valid assumption   
    vals <- lapply(var_names, get) %>% unlist # I assume that each value stored is a vector of length 1, this might not be a valid assumption
    tibble(path = ., value = vals)
  } -> all_files_DIAG_CLEAN

相关问题