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 tar_make_future(): variables declared in _targets.r can conflict with functions from imported packages #761

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

Comments

@mkoohafkan
Copy link

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 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, year) {
  data.frame(
    chosen_category = rep(category, 4),
    chosen_year = rep(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.
@wlandau
Copy link
Member

wlandau commented Feb 3, 2022

I can reproduce this with just future.callr (see below) so targets is not the source of the bug. Would you submit an issue to https://github.com/HenrikBengtsson/future.callr?

library(future)
library(future.callr)

# with a callr future
plan(callr)
f <- future({
  suppressPackageStartupMessages(library(lubridate))
  fun(year)
},
globals = list(fun = function(year) year, year = 2022)
)
value(f)
#> function (x) 
#> UseMethod("year")
#> <bytecode: 0x7fda22231148>
#> <environment: namespace:lubridate>

# with a multisession future
plan(multisession)
f <- future({
  suppressPackageStartupMessages(library(lubridate))
  fun(year)
},
globals = list(fun = function(year) year, year = 2022)
)
value(f)
#> [1] 2022

Created on 2022-02-03 by the reprex package (v2.0.1)

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

No branches or pull requests

2 participants