2

我正在尝试将使用专用环境(而不是全局环境)手册第 12.7.6.5 节中r_make()的建议调整为与.

我所做的是修改_drake.R配置脚本如下:

envir <- new.env(parent = globalenv())
source("R/packages.R", local = envir)  # Load your packages, e.g. library(drake).
source("R/functions.R", local = envir) # Define your custom code as a bunch of functions.
source("R/plan.R", local = envir)      # Create your drake plan.
drake_config(plan, envir = envir)

对于任何包、功能和计划。当我跑

library(drake)
r_make()

我得到:

Error in if (nrow(plan) < 1L) { : argument is of length zero

Error: <callr_status_error: callr subprocess failed: argument is of length zero>
-->
<callr_remote_error in if (nrow(plan) < 1L) { ...:
 argument is of length zero>
 in process 1598 

See `.Last.error.trace` for a stack trace.

我错过了什么吗?

4

1 回答 1

1

如果你已经在使用r_make(),你很可能不需要费心envir。因为r_make()开始和结束都在自己的隔离callr::r()进程中,所以主会话的全局环境已经受到保护。事实上,它r_make()比环境再现性要好得多envir,因此您已经走在正确的轨道上。

但是如果你还想使用envir,请确保计划是在调用的环境中定义的drake_config():即运行的会话的全局环境_drake.R。因此,您可以调用drake_config(envir$plan, envir = envir)或编写source("plan.R")而不是source("plan.R", local = envir).

例子:

writeLines(
  c(
    "library(drake)",
    "plan <- drake_plan(x = 1)"  
  ),
  "plan.R"
)
writeLines(
  c(
    "envir <- new.env(parent = globalenv())",
    "source(\"plan.R\", local = envir)",
    "ls() # does not contain the plan",
    "ls(envir) # contains the plan",
    "drake_config(envir$plan, envir = envir)"
  ),
  "_drake.R"
)

cat(readLines("plan.R"), sep = "\n")
#> library(drake)
#> plan <- drake_plan(x = 1)

cat(readLines("_drake.R"), sep = "\n")
#> envir <- new.env(parent = globalenv())
#> source("plan.R", local = envir)
#> ls() # does not contain the plan
#> ls(envir) # contains the plan
#> drake_config(envir$plan, envir = envir)

library(drake)
r_make()
#> [32mtarget[39m x

reprex 包(v0.3.0)于 2020 年 1 月 13 日创建

writeLines(
  c(
    "library(drake)",
    "plan <- drake_plan(x = 1)"  
  ),
  "plan.R"
)
writeLines(
  c(
    "envir <- new.env(parent = globalenv())",
    "source(\"plan.R\") # source into global envir",
    "ls()",
    "ls(envir)",
    "drake_config(plan, envir = envir)"
  ),
  "_drake.R"
)

cat(readLines("plan.R"), sep = "\n")
#> library(drake)
#> plan <- drake_plan(x = 1)

cat(readLines("_drake.R"), sep = "\n")
#> envir <- new.env(parent = globalenv())
#> source("plan.R") # source into global envir
#> ls()
#> ls(envir)
#> drake_config(plan, envir = envir)

library(drake)
r_make()
#> [32mtarget[39m x

reprex 包(v0.3.0)于 2020 年 1 月 13 日创建

于 2020-01-13T13:51:28.403 回答