6

是否可以锁定全局环境并仍然允许.Random.seed设置或删除?的默认行为lockEnvironment()对于我的用例来说过于激进。

lockEnvironment(globalenv())
rnorm(10)
#> Error in rnorm(10) : cannot add bindings to a locked environment
rm(.Random.seed)
#> Error in rm(.Random.seed) : 
#>   cannot remove bindings from a locked environment

背景

drake7.0.0 版将有一个新的保障措施来保护再现性。

plan <- drake_plan(
  x = {
    data(mtcars)
    mtcars$mpg
  },
  y = mean(x)
)

plan
#> # A tibble: 2 x 2
#>   target command                            
#>   <chr>  <expr>                             
#> 1 x      {     data(mtcars)     mtcars$mpg }
#> 2 y      mean(x)

make(plan)
#> target x
#> fail x
#> Error: Target `x` failed. Call `diagnose(x)` for details. Error message:
#>   cannot add bindings to a locked environment. 
#> One of your targets tried to modify your environment,
#> which could invalidate other targets
#> and undermine reproducibility (example: 
#> https://github.com/ropensci/drake/issues/664#issuecomment-453163562).
#> Beware <<-, ->>, attach(), data(), and side effects in general.
#> Use make(lock_envir = FALSE) to avoid this error (not recommended).

错误来自对 的调用data(mtcars)。构建的行为x本身就会改变x's 的依赖关系。没有护栏,工作流程就会自行失效。

make(plan, lock_envir = FALSE)
#> target x
#> target y

make(plan, lock_envir = FALSE)
#> target x

但是有了护栏,我们会遇到像https://github.com/ropensci/drake/issues/749https://github.com/ropensci/drake/issues/675#issuecomment-458222414这样的边缘案例。

4

0 回答 0