以编程方式创建目标列表时找不到对象
object not found when creating targets list programmatically
我正在尝试通过 R 包中的函数以编程方式生成 {targets} 列表。
get_pipeline <- function(which_countries) {
countries <- NULL # avoid R CMD CHECK warning
print(which_countries) # Shows that which_countries is available
list(
targets::tar_target(
name = countries,
command = which_countries # But here, which_countries is not found
)
)
}
_targets.R
文件如下所示:
library(targets)
couns <- c("USA", "GBR")
TargetsQuestions::get_pipeline(couns)
我看到以下错误:
> tar_make()
[1] "USA" "GBR"
Error in enexpr(expr) : object 'which_countries' not found
Error in `tar_throw_run()`:
! callr subprocess failed: object 'which_countries' not found
请注意,which_countries
变量是可打印的,但在对 tar_target
的调用中找不到。
如何才能成功创建 countries
目标,使其包含向量 c("USA", "GBR")
?
此代码位于 https://github.com/MatthewHeun/TargetsQuestions 的 GitHub 存储库中。重现:
git clone https://github.com/MatthewHeun/TargetsQuestions
- 在 RStudio 中构建包。
targets::tar_make()
在控制台。
提前感谢您的任何建议!
感谢@landau 指向 https://wlandau.github.io/targetopia/contributing.html#target-factories which in turn points to the metaprogramming section of Advanced R at https://adv-r.hadley.nz/metaprogramming.html。
解决方案原来是:
get_pipeline <- function(which_countries) {
list(
targets::tar_target_raw(
name = "countries",
# command = which_countries # which_countries must have length 1
# command = !!which_countries # invalid argument type
command = rlang::enexpr(which_countries) # Works
)
)
}
与 _targets.R
像这样:
library(targets)
couns <- c("USA", "GBR")
TargetsQuestions::get_pipeline(couns)
命令tar_make()
和tar_read(countries)
,给出
[1] "USA" "GBR"
符合预期!
我正在尝试通过 R 包中的函数以编程方式生成 {targets} 列表。
get_pipeline <- function(which_countries) {
countries <- NULL # avoid R CMD CHECK warning
print(which_countries) # Shows that which_countries is available
list(
targets::tar_target(
name = countries,
command = which_countries # But here, which_countries is not found
)
)
}
_targets.R
文件如下所示:
library(targets)
couns <- c("USA", "GBR")
TargetsQuestions::get_pipeline(couns)
我看到以下错误:
> tar_make()
[1] "USA" "GBR"
Error in enexpr(expr) : object 'which_countries' not found
Error in `tar_throw_run()`:
! callr subprocess failed: object 'which_countries' not found
请注意,which_countries
变量是可打印的,但在对 tar_target
的调用中找不到。
如何才能成功创建 countries
目标,使其包含向量 c("USA", "GBR")
?
此代码位于 https://github.com/MatthewHeun/TargetsQuestions 的 GitHub 存储库中。重现:
git clone https://github.com/MatthewHeun/TargetsQuestions
- 在 RStudio 中构建包。
targets::tar_make()
在控制台。
提前感谢您的任何建议!
感谢@landau 指向 https://wlandau.github.io/targetopia/contributing.html#target-factories which in turn points to the metaprogramming section of Advanced R at https://adv-r.hadley.nz/metaprogramming.html。
解决方案原来是:
get_pipeline <- function(which_countries) {
list(
targets::tar_target_raw(
name = "countries",
# command = which_countries # which_countries must have length 1
# command = !!which_countries # invalid argument type
command = rlang::enexpr(which_countries) # Works
)
)
}
与 _targets.R
像这样:
library(targets)
couns <- c("USA", "GBR")
TargetsQuestions::get_pipeline(couns)
命令tar_make()
和tar_read(countries)
,给出
[1] "USA" "GBR"
符合预期!