如果你已经在使用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 日创建