锁定环境,但不锁定.seed

lan*_*dau 6 r ropensci r-environment drake-r-package

是否可以锁定全局环境并且仍然允许.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
Run Code Online (Sandbox Code Playgroud)

背景

drake 7.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).
Run Code Online (Sandbox Code Playgroud)

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

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

make(plan, lock_envir = FALSE)
#> target x
Run Code Online (Sandbox Code Playgroud)

但是在使用护栏的情况下,我们遇到了诸如https://github.com/ropensci/drake/issues/749https://github.com/ropensci/drake/issues/675#issuecomment-458222414之类的边缘案例。