有时候,我发现有一个像无证选择R里面的函数use.names中c:
x <- c(a = 1, b = 2, c = 3, d = 4)
y <- c(e = 5, f = 6, g = 7, e = 8, h = 9)
print(c(x, y))
# a b c d e f g e h
# 1 2 3 4 5 6 7 8 9
print(c(x, y, i = 10))
# a b c d e f g e h i
# 1 2 3 4 5 6 7 8 9 10
print(c(x, y, use.names = F))
# [1] 1 2 3 4 5 6 7 8 9
Run Code Online (Sandbox Code Playgroud)
有没有办法检查这个.Internal或.Primitive功能选项?它们的使用是否有任何危险,比如API的随机变化?
今天(2021 年,R4.1)该use.names选项已正式记录。
检查 .Internal 或 .Primitive R 函数实现的唯一方法是检查 R 源代码 - 对于此特定选项,这里是确切的行。
一般来说,未记录的 API 或 API 部分不是开发人员和用户之间合同的一部分,因此可能随时更改。这个选项可能在某个时候是一个实验性功能,记录它使其成为 R 的正式部分。或者,它可能是一个无意的疏忽,稍后被修复。在跟踪了 R 开发过程之后,在我看来,这两种选择都是同样可能的。
编辑:我最近了解到Jim Hester 的“查找”R 包,它使用 github API 和一些启发式方法来检查R 会话中的R 源。例子:
> devtools::install_github("jimhester/lookup")
Downloading GitHub repo jimhester/lookup@HEAD
These packages have more recent versions available.
It is recommended to update all of them.
Which would you like to update?
1: All
2: CRAN packages only
3: None
4: gh (1.3.0 -> f75b0e498...) [GitHub]
5: memoise (2.0.1 -> 3b0bf3e5f...) [GitHub]
Enter one or more numbers, or an empty line to skip updates: 3 # <- just to save time
...
> library("lookup")
...
> is.list # for example
Run Code Online (Sandbox Code Playgroud)
这将打开一个包含相关 C 代码的编辑器窗口。请注意,仅检查最新的 R 源,它们通常与您实际运行的 R 源略有不同。