更新嵌入式数据,例如sysdata.rda [英] Updating embedded data, for example sysdata.rda

查看:104
本文介绍了更新嵌入式数据,例如sysdata.rda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对CRAN的最新提交被退回,因为我对全球环境负有任务,但现在对此感到不满意.

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

我有一个嵌入式数据集(sysdata.rda),其中包含基于用户所处状态(如美国)的配置参数.我希望当新用户使用该程序时,该嵌入式数据集可更新.我以前在用户使用的初始函数中更新了此数据,并通过全局分配使其可供用户访问.

I have an embedded data set (sysdata.rda) that contains configuration parameters based upon state (as in United States) the user resides. I have wanted this embedded data set to be updatable when a new user uses the program. I previously updated this data in the initial function the user uses and made it accessible to the user via global assignment.

我正在努力弄清楚如何更新此嵌入式数据,并将其设置为用户在剩余会话中使用的默认数据.

I am struggling to figure out how to update this embedded data and make it the default data that the user uses for the remainder of their session.

以前,我将数据存储在/data中,最近将其切换到/R/sysdata.rda,因为它似乎更适合该语言环境.现在我不太确定.

Previously I housed the data in /data and recently switched it to /R/sysdata.rda as it seemed more suited for that locale. Now I'm not so sure.

任何帮助表示赞赏

推荐答案

关键是要在除全局环境之外的其他环境中进行分配.有两种基本技术,使用local()<<-或显式创建新环境:

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

使用local()有点复杂,并且需要使用<<-

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

有关<<-工作方式的更多信息,请参见 https://github.com/hadley/devtools/wiki/environments ,在分配:将名称绑定到值"部分.

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

这篇关于更新嵌入式数据,例如sysdata.rda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆