问题
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