R:您怎么称呼::和:::运算符,它们有何不同? [英] R: What do you call the :: and ::: operators and how do they differ?

查看:148
本文介绍了R:您怎么称呼::和:::运算符,它们有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道:::::运算符的功能在R中如何区别.

I'm wondering how the functions of the :: and ::: operators differ in R.

但是,我无法弄清楚这些运算符的名称,因此Google或SO搜索没有被证明是有用的.当我在R中尝试?::时,也会出现错误.

However, I can't figure out what these operators are called and so a google or SO search has not proven helpful. I also get an error when I try ?:: in R.

所以...

  1. :::::运算符叫什么?

:::::有何区别? (即每个到底是什么做什么)?

How do :: and ::: differ? (i.e., what exactly does each do)?

推荐答案

事实证明,有一种独特的方法可以访问诸如冒号这样的运算符的帮助信息:在运算符周围加上引号. [例如?'::'help(":::")].

It turns out there is a unique way to access help info for operators such as these colons: add quotations marks around the operator. [E.g., ?'::' or help(":::")].

  • 此外,反引号(即`)也可以代替引号.

该问题的答案可以在双冒号和三冒号运算符"帮助页面上找到(请参阅

The answer to the question can be found on the help page for "Double Colon and Triple Colon Operators" (see here).

对于包pkg,pkg :: name返回名称空间pkg中 exported 变量名的值,而pkg :: name返回内部变量名称.如果未在调用之前加载程序包名称空间,则将加载该程序包名称空间,但是该程序包将不会附加到搜索路径.

For a package pkg, pkg::name returns the value of the exported variable name in namespace pkg, whereas pkg:::name returns the value of the internal variable name. The package namespace will be loaded if it was not loaded before the call, but the package will not be attached to the search path.

通过检查每个代码可以看出差异:

The difference can be seen by examining the code of each:

> `::`
function (pkg, name) 
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    getExportedValue(pkg, name)
}
<bytecode: 0x00000000136e2ae8>
<environment: namespace:base>

> `:::`
function (pkg, name) 
{
    pkg <- as.character(substitute(pkg))
    name <- as.character(substitute(name))
    get(name, envir = asNamespace(pkg), inherits = FALSE)
}
<bytecode: 0x0000000013482f50>
<environment: namespace:base>

::调用getExportedValue(pkg, name)返回包名称空间中 exported 变量name 的值.

:: calls getExportedValue(pkg, name), returning the value of the exported variable name in the package's namespace.

::: 调用get(name, envir = asNamespace(pkg), inherits = FALSE),在包的命名空间环境中搜索对象name,并返回内部的值变量name .

::: calls get(name, envir = asNamespace(pkg), inherits = FALSE), searching for the object name in the Namespace environment of the package, and returning the value of the internal variable name.

那么,命名空间到底是什么?

网站很好地解释了R中名称空间的概念重要的是:

This site does a good job of explaining the concept of namespaces in R. Importantly:

顾名思义,名称空间为名称"提供空间".它们为查找与名称关联的对象的值提供了上下文.

As the name suggests, namespaces provide "spaces" for "names". They provide a context for looking up the value of an object associated with a name.

这篇关于R:您怎么称呼::和:::运算符,它们有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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