Problem saving targets object to data/ in a user-defined R package #588
-
Hello! I absolutely love Thanks so much! MotivationI want to write my analysis as an R package, and save the output object to ProblemWhen I used the
QuestionWhat is the preferred way to save a |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
The difficulty comes from how write_to_data <- function (...) {
args <- list(...)
envir <- environment()
purrr::walk2(
.x = names(args),
.y = args,
~assign(x = .x, value = .y, envir = envir)
)
usethis::use_data(..., overwrite = TRUE)
file.path("data", fs::path_ext_set(names(args)[1], ".rda"))
} This assumes you name your arguments, e.g. I would also advise |
Beta Was this translation helpful? Give feedback.
The difficulty comes from how
targets
handles in-memory data. To prevent accidental side-effects, in-memory targets are kept in a special environment that inherits fromtar_option_get("envir")
. To get around this, you can make sure each element of your data exists as a variable in the call frame ofwrite_to_data()
.This assumes you name your arguments, e.g.
write_to_data(my_data = my_data)
.I would also …