Updating embedded data, for example sysdata.rda

前端 未结 2 1975
长发绾君心
长发绾君心 2021-02-04 13:05

My latest submission to CRAN got bounced back because I have assignments to the global environment which is now frowned upon.

I have an embedded data set (sysdata.rda) t

2条回答
  •  轮回少年
    2021-02-04 14:04

    The key is to do the assignment in an environment other than the global environment. There are two basic techniques, using local() and <<- or explicitly creating a new environment:

    Working with an explicit environment is straightforward: create the environment and then assign into it like a list:

    my_opts <- new.env(parent = emptyenv())
    set_state <- function(value) my_opts$state <- value
    get_state <- function() my_opts$state
    

    Using local() is a little more complex, and requires some tricks with <<-

    set_state <- NULL
    get_state <- NULL
    
    local({
      state <- NULL
      get_state <<- function() state
      set_state <<- function(value) state <<- value
    })
    

    For more info on how <<- works see https://github.com/hadley/devtools/wiki/environments, in the "Assignment: binding names to values" section.

提交回复
热议问题