你什么时候想在 R 中设置新环境

发布于 2024-10-06 18:06:19 字数 172 浏览 0 评论 0原文

根据 R 编程风格的讨论,我看到有人曾经说过他将所有自定义函数放入一个新环境中并附加它。我还记得 R 环境可能用作哈希表。这样的风格好吗?您什么时候想将数据/功能放入新环境?或者只是使用 .GlobalEnv 等等?

编辑把我问题的第二部分放回去: 如何检查不同环境的同名变量?

per the discussion of R programming styles, I saw someone once said he puts all his custom function into a new environment and attach it. I also remember R environment maybe used as a hash table. Is this good style? When do you want to put your data/functions into a new enviroment? Or just use the.GlobalEnv whatever?

EDIT put my second part of question back:
how to inspect same name variable for different environments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

栩栩如生 2024-10-13 18:06:19

Martin Mächler 建议,这是您可能需要考虑 attach() 的一次,尽管他是在附加 .Rdata 的上下文中建议的。 > 文件到搜索路径,但你的 Q 本质上是一样的。

优点是你不会用函数扰乱全局环境,这些函数可能会被意外覆盖等。虽然我不会说这种风格不好,但你最好将自定义函数粘贴到你自己的个人环境中R 包。是的,这会产生一些设置包结构和提供一些文档以允许安装包的开销,但从长远来看,这是一个更好的解决方案。有了像 roxygen 这样的工具,这个过程就变得更容易启动。

就我个人而言,在使用 R 的 10 多年里,我还没有发现需要摆弄环境;到目前为止,记录良好的脚本加载、处理和分析数据,并始终自行清理,这对我很有帮助。


对于问题第二部分(现已删除)的另一个建议是使用 with() (遵循 @Joshua 的示例):

> .myEnv <- new.env()
> .myEnv$a <- 2
> a <- 1
> str(a)
 num 1
> ls.str(.myEnv, a)
a :  num 2
> str(.myEnv$a)
 num 2
> with(.myEnv, a)
[1] 2
> a
[1] 1

Martin Mächler suggests that this is the one time you might want to consider attach(), although he suggested it in the context of attaching a .Rdata file to the search path but your Q is essentially the same thing.

The advantage is that you don't clutter the global environment with functions, that might get overwritten accidentally etc. Whilst I wouldn't go so far as to call this bad style, you might be better off sticking your custom functions into your own personal R package. Yes, this will incur a bit of overhead of setting up the package structure and providing some documentation to allow the package to be installed, but in the longer term this is a better solution. With tools like roxygen this process is getting easier to boot.

Personally, I haven't found a need for fiddling with environments in 10+ years of using R; well documented scripts that load, process and analyse data, cleaning up after themselves all the while have served me well thus far.


Another suggestion for the second part of your question (now deleted) is to use with() (following on from @Joshua's example):

> .myEnv <- new.env()
> .myEnv$a <- 2
> a <- 1
> str(a)
 num 1
> ls.str(.myEnv, a)
a :  num 2
> str(.myEnv$a)
 num 2
> with(.myEnv, a)
[1] 2
> a
[1] 1
一城柳絮吹成雪 2024-10-13 18:06:19

如果您的数据和代码生态系统已经足够大,以至于您正在考虑将其隔离在一个环境中,那么您最好创建一个包。包为您提供了更多支持:

  • 通过将代码和数据分离到文件中来管理日益庞大和复杂的项目,这样一次需要挖掘的内容就会减少。

  • 包使您可以轻松地将您的工作交给其他人,以便他们可以使用您的代码和数据。

  • 包为文档和报告提供额外支持。

为 R 设置包非常简单,只需调用 package.sculpture() 即可,我从事的每个项目都将其代码和数据存储在包中。

我使用环境的唯一一次是当我需要隔离某些代码的运行时,通常是其他人编写的脚本,这样它的副作用和变量名称就不会与我的代码交叉。我通过 evalq(source('someScript.R', local=TRUE), SomeEnvironment) 来完成此操作。

If your ecosystem of data and code has grown large enough that you are considering isolating it in an environment, you are better off creating a package. A package gives you much more support for:

  • Managing a project that is growing large and complex by separating code and data into files so there is less to dig through at one time.

  • A package makes it dead simple to hand off your work to someone else so they can use your code and data.

  • A package provides additional support for documentation and reporting.

Setting up a package for R is so easy, just call package.skeleton(), that every project I work on gets its code and data stored in a package.

The only time I use environments is when I need to isolate a run of some code, usually a script written by someone else, so that it's side effects and variable names don't get crossed with mine. I do this by evalq(source('someScript.R', local=TRUE), SomeEnvironment).

王权女流氓 2024-10-13 18:06:19

要回答您的第二个问题(您现在已删除),请使用 ls.str,或者仅使用 $ 访问环境中的对象:

.myEnv <- new.env()
.myEnv$a <- 2
a <- 1
str(a)
ls.str(.myEnv, a)
str(.myEnv$a)

To answer your second question (that you've now deleted), use ls.str, or just access the object in the environment with $:

.myEnv <- new.env()
.myEnv$a <- 2
a <- 1
str(a)
ls.str(.myEnv, a)
str(.myEnv$a)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文