R 包中自定义工作流函数的 `tar_script()` 中的参数求值问题

问题描述 投票:0回答:1

我正在 R 包中开发一个函数,旨在运行 targets 工作流程。该软件包当前托管在 GitHub 上。这是

_targets.R
脚本的简化版本,手动编写时可以使用:

#remotes::install_github("derek-corcoran-barrios/SpeciesPoolR")
# If you want to use the package

# _targets.R file
library(targets)
library(crew)

f <- system.file("ex/Species_List.csv", package="SpeciesPoolR")

tar_option_set(packages = c("SpeciesPoolR"),
               controller = crew_controller_local(workers = 4),
               error = "null")  # Skip non-debugging outdated targets

list(
  tar_target(file, command = f, format = "file"),
  tar_target(data, get_data(file,
                            filter = quote(
                              Kingdom == "Plantae" &
                              Class == "Liliopsida"
                            ))),
  tar_target(Clean, SpeciesPoolR::Clean_Taxa(data$Species)),
  tar_target(Count_Presences,
             SpeciesPoolR::count_presences(Clean, country = "DK"),
             pattern = map(Clean))
)

我正在尝试将此脚本封装到我的包中的函数中,允许用户自定义参数,例如工作人员数量或文件路径。这是我写的函数:

run_workflow <- function(workers = 2,
                         error = "null",
                         file_path) {

  data <- Clean <- clean_species <- NULL

  controller <- substitute(crew::crew_controller_local(workers = workers))
  error_val <- substitute(error)
  file_path_val <- substitute(file_path)

  # Set tar_option_set for the workflow
  targets::tar_script({
    targets::tar_option_set(
      packages = c("SpeciesPoolR"),
      controller = eval(controller),
      error = eval(error_val)
    )

    # Define the targets pipeline
    targets <- list(
      targets::tar_target(file, command = eval(file_path_val), format = "file"),
      targets::tar_target(data, get_data(file)),
      targets::tar_target(Clean, clean_species(data))
    )
  }, ask = FALSE)

  # Run the workflow
  targets::tar_make(targets)
}

但是,当我尝试运行此功能时:

library(SpeciesPoolR)

f <- system.file("ex/Species_List.csv", package="SpeciesPoolR")

run_workflow(workers = 4,
             error = "null",
             file_path = f)

我遇到错误,因为在

tar_script()
调用中未正确评估参数。生成的
_targets.R
文件如下所示:

library(targets)
targets::tar_option_set(packages = c("SpeciesPoolR"), controller = eval(controller), 
    error = eval(error_val))
targets <- list(targets::tar_target(file, command = eval(file_path_val), 
    format = "file"), targets::tar_target(data, get_data(file)), 
    targets::tar_target(Clean, clean_species(data)))

如您所见,

controller
应该被评估为
crew_controller_local(workers = 4)
,但实际上,它只是
eval(controller)
file_path
也会出现同样的问题。

问题: 如何确保

tar_script()
中的参数得到正确评估,以便将正确的值传递到目标工作流程?

任何有关如何解决此问题的意见或建议将不胜感激!

r r-package targets-r-package
1个回答
0
投票

解决了,我主要将函数从tar_script更改为tar_helper

run_workflow <- function(workers = 2,
                         error = "null",
                         file_path,
                         filter = NULL
                         ) {

  data <- Clean <- NULL

  # Write the script using tar_helper()
  targets::tar_helper(
    path = "_targets.R",
    code = {

      targets::tar_option_set(
        packages = c("SpeciesPoolR"),
        controller =  crew::crew_controller_local(workers = !!workers),
        error = !!error
      )

      list(
        targets::tar_target(file, command = !!file_path, format = "file"),
        targets::tar_target(data, get_data(file, filter = !!rlang::enquo(filter))),
        targets::tar_target(Clean, SpeciesPoolR::Clean_Taxa(data$Species))
      )
    },
    tidy_eval = TRUE  # This ensures the !! operators work as expected
  )

  # Run the workflow
  targets::tar_make()
}
© www.soinside.com 2019 - 2024. All rights reserved.