R如何将范围内的名称限制为我明确创建的名称?

时间:2021-05-18 23:22:15

I thought that it would be enough to use fully qualified names to avoid polluting my scope with names I did not explicitly introduce, but apparently, with R, this is not the case.

我认为使用完全限定名称来避免使用我没有明确介绍的名称来污染我的范围就足够了,但显然,对于R,情况并非如此。

For example,

例如,

% R_PROFILE_USER= /usr/bin/R --quiet --no-save --no-restore
> ls(all = TRUE)
character(0)
> load("/home/berriz/_/projects/fda/deseq/.R/data_for_deseq.RData")
> ls(all = TRUE)
[1] "a" "b" "c"
> ?rlog
No documentation for ‘rlog’ in specified packages and libraries:
you could try ‘??rlog’

So far, so good. In particular, as the last command shows, the interpreter knows nothing of rlog.

到现在为止还挺好。特别是,正如最后一个命令所示,解释器对rlog一无所知。

But after I run

但是在我跑完之后

> d <- DESeq2::DESeqDataSetFromMatrix(countData = a, colData = b, design = c)

...then, henceforth, the command ?rlog will produce a documentation page for a function I did not explicitly introduce into the environment (and did not refer to with a fully qualified name).

...那么,从此以后,命令?rlog将为我没有明确引入环境的函数生成一个文档页面(并没有引用完全限定的名称)。

I find this behavior disconcerting.

我发现这种行为令人不安。

In particular, I don't know when some definition I have explicitly made will be silently shadowed as a side-effect of some seemingly unrelated command.

特别是,我不知道我明确提出的某些定义何时会被默默地视为一些看似无关的命令的副作用。

How can I control what the environment can see?

如何控制环境可以看到的内容?

Or to put it differently, how can I prevent side effects like the one illustrated above?

或者换句话说,我如何防止上面所示的副作用?

1 个解决方案

#1


1  

Not sure if "scope" means the same thing in R as it may to other languages. R uses "environments" (see http://adv-r.had.co.nz/Environments.html for detailed explanation). Your scope in R includes all environments that are loaded, and as you have discovered, the user doesn't explicitly control every environment that is loaded.

不确定“范围”在R中是否与其他语言相同。 R使用“环境”(有关详细说明,请参阅http://adv-r.had.co.nz/Environments.html)。 R中的范围包括所有已加载的环境,并且如您所发现的,用户不会显式控制所加载的每个环境。

For example,

例如,

ls()

lists the objects in your default environment '.GlobalEnv'

列出默认环境中的对象'.GlobalEnv'

search()

lists the currently loaded environments.

列出当前加载的环境。

ls(name='package.stats')

In default R installations, 'package:stats' is one of the environments loaded on startup.

在默认的R安装中,'package:stats'是启动时加载的环境之一。

By default, everything you create is stored in the global environment.

默认情况下,您创建的所有内容都存储在全局环境中。

ls(name='.GlobalEnv')

You can explicitly reference objects you create by referencing their environment with the $ syntax.

您可以通过使用$语法引用其环境来显式引用您创建的对象。

x <- c(1,2,3)
.GlobalEnv$x

#1


1  

Not sure if "scope" means the same thing in R as it may to other languages. R uses "environments" (see http://adv-r.had.co.nz/Environments.html for detailed explanation). Your scope in R includes all environments that are loaded, and as you have discovered, the user doesn't explicitly control every environment that is loaded.

不确定“范围”在R中是否与其他语言相同。 R使用“环境”(有关详细说明,请参阅http://adv-r.had.co.nz/Environments.html)。 R中的范围包括所有已加载的环境,并且如您所发现的,用户不会显式控制所加载的每个环境。

For example,

例如,

ls()

lists the objects in your default environment '.GlobalEnv'

列出默认环境中的对象'.GlobalEnv'

search()

lists the currently loaded environments.

列出当前加载的环境。

ls(name='package.stats')

In default R installations, 'package:stats' is one of the environments loaded on startup.

在默认的R安装中,'package:stats'是启动时加载的环境之一。

By default, everything you create is stored in the global environment.

默认情况下,您创建的所有内容都存储在全局环境中。

ls(name='.GlobalEnv')

You can explicitly reference objects you create by referencing their environment with the $ syntax.

您可以通过使用$语法引用其环境来显式引用您创建的对象。

x <- c(1,2,3)
.GlobalEnv$x