assigning by reference into loaded package datasets

前端 未结 2 893
野性不改
野性不改 2020-12-03 07:40

I am in the process of creating a package that uses a data.table as a dataset and has a couple of functions which assign by reference using :=.

2条回答
  •  遥遥无期
    2020-12-03 08:18

    Another solution is to use inst/extdata to save the rda file (which would contain any number of data.table objects) and have a file DT.r within the data subdirectory

    # get the environment from the call to `data()`
    env <- get('envir', parent.frame(1))
    # load the data
    load(system.file('extdata','DT.rda', package= 'foo'), envir = env)
    # overallocate (evaluating in correct environment)
    if(require(data.table)){
    # the contents of `DT.rda` are known, so write out in full
      evalq(alloc.col(DT), envir = env)
    
    }
    # clean up so `env` object not present in env environment after calling `data(DT)`
    rm(list = c('env'), envir = env)
    
    
    
    }
    

提交回复
热议问题