[英]How to save multiple objects as .Rdata files with purrr
I have a list of objects in my R envir and I would like to save them as correspondent .Rdata
files.我的 R 环境中有一个对象列表,我想将它们保存为对应
.Rdata
文件。 I know how to save them individually, but I would like to do it iteratively with purrr
(eg using map
or walk
?).我知道如何单独保存它们,但我想用
purrr
迭代地做(例如使用map
或walk
?)。 I tried several times and I always get an error.我试了几次,总是报错。
# Example dfs
my_mtcars <- mtcars
my_orange <- Orange
list_dfs <- list (my_mtcars = my_mtcars, my_orange = my_orange)
# Individually I would do:
saveRDS(object = my_mtcars, file = paste0("my_mtcars", ".RDS"))
# Here is one of my [WRONG] attempts
n <- 1:2
purrr::walk2(.x = list_dfs, .y =n, ~saveRDS(.x, file = paste0(names(list_dfs[n]), ".RDS")))
Any ideas?有任何想法吗?
I think better option would be to use imap
/ iwalk
here which passes the data ( .x
) as well as name ( .y
) to the function.我认为更好的选择是在此处使用
imap
/ iwalk
,它将数据( .x
)和名称( .y
)传递给函数。
purrr::imap(list_dfs, ~saveRDS(.x, file = paste0(.y, ".RDS")))
With iwalk
-与
iwalk
-
purrr::iwalk(list_dfs, ~saveRDS(.x, file = paste0(.y, ".RDS")))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.