【发布时间】:2020-04-29 22:32:27
【问题描述】:
我正在尝试将something 放在 CRAN 上,它允许用户通过在搜索路径上创建一个类似的静态对象来操纵反应性闪亮对象。我知道我无法写入全局环境(它当前所做的),但我不确定如何在函数执行后让对象持续存在。
store_it <- function() {
env <- new.env()
assign("x", runif(10), env)
assign("iris_df", head(iris), env)
# View(env)
env
}
# how I want to use it, doesn't work
store_it() # <environment: 0x0000012bd8959cb0>
x # Error: object 'x' not found
iris_df # Error: object 'iris_df' not found
# works
e <- attach(store_it())
x
iris_df
它做我想做的事,但我不喜欢它不断向搜索路径添加环境:
e <- attach(store_it())
# The following objects are masked from store_it() (pos = 3):
# iris_df, x
e <- attach(store_it())
# The following objects are masked from store_it() (pos = 4):
# iris_df, x
e <- attach(store_it())
# The following objects are masked from store_it() (pos = 5):
# iris_df, x
这样做的正确方法是什么?我希望用户只写store_it()。如果attach() 是正确的方法,我该如何将它放在函数中,这样它就不会不断创造新的环境?请记住,解决方案需要通过 CRAN 的政策。提前致谢。
注意:有人可能会指出我过去曾问过类似的问题。我发了一个新帖子,因为这个问题更具体。 Package environment manipulation and submitting to CRAN
【问题讨论】: