Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scoping issue with future.callr and tar_make_future(): variables declared in _targets.r can conflict with functions from imported packages #23

Open
mkoohafkan opened this issue Feb 3, 2022 · 1 comment

Comments

@mkoohafkan
Copy link

mkoohafkan commented Feb 3, 2022

Originally posted in ropensci/targets#761

In this example, variables defined in _targets.r conflict with identically-named functions imported by packages specified via tar_option_set(). The following example reproduces the error with plan(callr) and tar_make_future(). The issue does not occur when calling tar_make() or using plan(sequential). In this example, a variable year declared in _targets.r conflicts with the function lubridate::year().

_targets.r

library(targets)
library(tarchetypes)
library(future)
library(future.callr)

source("R/test.r")

plan(callr)

tar_option_set(packages = c("lubridate"))

year = 2022
categories = data.frame(category = LETTERS[1:5])


tar_map(
  values = categories,
  names = "category",
  tar_target("test", test_fun(category, year),
    iteration = "list")
)

R/test.r

test_fun = function(category, input_year) {
  data.frame(
    chosen_category = rep(category, 4),
    chosen_year = rep(input_year, 4),
    value = rnorm(4)
  )
}

console

targets::tar_make_future(workers = 2)
## start target test_A
## start target test_B
## error target test_A
## end pipeline
## Error in tar_throw_run(target$metrics$error) :
##   attempt to replicate an object of type 'closure'
## Error in `tar_throw_run()`:
## ! callr subprocess failed: attempt to replicate an object of type 'closure'
## Visit https://books.ropensci.org/targets/debugging.html for debugging advice.
## Run `rlang::last_error()` to see where the error occurred.
@franzbischoff
Copy link

Just crossed with this by chance...
I think this may have some relation with tidyevaluation. As the variable year is global, it may collide with the function name. Try to use dplyr::all_of(year) instead and see if it works.

BR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants