How to use devtools::use_data on a list of data frames?

狂风中的少年 提交于 2021-01-26 06:49:31

问题


I have a series of data frames that I want to save as individual .rda files in my package.

I could use devtools::use_data(my.df1, my.df2...) but I don't have a named object for each data frame, they are all stored in a big list.

What I would like is to do is to call use_data for each list element and use the list name for the .rda file name. But when I do the following, I have an error message:

> lapply(my.list, devtools::use_data, overwrite = TRUE)
Error: Can only save existing named objects

How can I do this?


回答1:


The use_data function seems to be very odd indeed requiring that an unquoted name is passed as a parameter that points to the object you want to save. This isn't conducive to working with objects in lists. But here is a possible solution with walk2 from purrr (though you could probably we-write with an mapply() if you want to use just base R)

library(purrr)
library(devtools)

walk2(my.list, names(my.list), function(obj, name) {
  assign(name, obj)
  do.call("use_data", list(as.name(name), overwrite = TRUE))
})



回答2:


You loop Assign(my.list, newNameOfYourList) over your list of lists. Then use devtools::use_data(newNameOfYourList, overwrite = TRUE).



来源:https://stackoverflow.com/questions/49673667/how-to-use-devtoolsuse-data-on-a-list-of-data-frames

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!