Skip to content

Problem saving targets object to data/ in a user-defined R package #588

Closed Answered by wlandau
luke-a-rogers asked this question in Help
Discussion options

You must be logged in to vote

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 from tar_option_get("envir"). To get around this, you can make sure each element of your data exists as a variable in the call frame of write_to_data().

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. write_to_data(my_data = my_data).

I would also …

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@luke-a-rogers
Comment options

@luke-a-rogers
Comment options

@wlandau
Comment options

Answer selected by luke-a-rogers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants