Skip to content

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

Open
@mkoohafkan

Description

@mkoohafkan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions