R:您如何称呼 :: 和 :::: 运算符,它们有何不同?
R: What do you call the :: and ::: operators and how do they differ?
我想知道 R 中 ::
和 :::
运算符的功能有何不同。
但是,我无法弄清楚这些运算符的名称,因此 google 或 SO 搜索没有被证明有帮助。当我在 R 中尝试 ?::
时也出现错误。
所以...
::
和 :::
运算符叫什么?
::
和 :::
有何不同? (即,每个 究竟做了什么)?
事实证明,有一种独特的方法可以访问这些冒号等运算符的帮助信息:在运算符周围添加引号。 [例如,?'::'
或help(":::")
].
- 此外,反引号(即 ` )也可以代替引号。
双冒号运算符和三冒号运算符
问题的答案可以在 "Double Colon and Triple Colon Operators" 的帮助页面上找到(参见 here)。
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.
通过检查每个代码可以看出差异:
> `::`
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
在包的命名空间中。
:::调用get(name, envir = asNamespace(pkg), inherits = FALSE)
,在包的命名空间环境中搜索对象name
,并返回内部变量的值name
.
那么,命名空间到底是什么?
这篇 site 很好地解释了 R 中命名空间的概念。重要的是:
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 中 ::
和 :::
运算符的功能有何不同。
但是,我无法弄清楚这些运算符的名称,因此 google 或 SO 搜索没有被证明有帮助。当我在 R 中尝试 ?::
时也出现错误。
所以...
::
和:::
运算符叫什么?::
和:::
有何不同? (即,每个 究竟做了什么)?
事实证明,有一种独特的方法可以访问这些冒号等运算符的帮助信息:在运算符周围添加引号。 [例如,?'::'
或help(":::")
].
- 此外,反引号(即 ` )也可以代替引号。
双冒号运算符和三冒号运算符
问题的答案可以在 "Double Colon and Triple Colon Operators" 的帮助页面上找到(参见 here)。
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.
通过检查每个代码可以看出差异:
> `::`
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
在包的命名空间中。
:::调用get(name, envir = asNamespace(pkg), inherits = FALSE)
,在包的命名空间环境中搜索对象name
,并返回内部变量的值name
.
那么,命名空间到底是什么?
这篇 site 很好地解释了 R 中命名空间的概念。重要的是:
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.