锁定环境,但不锁定.seed

时间:2019-02-21 16:17:26

标签: r ropensci r-environment drake-r-package

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

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

背景

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).

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

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这样的极端情况。

0 个答案:

没有答案