我有以下功能供个人使用。它需要一个作者的名字才能看到我的机器上是否有任何包装。
authoredPackages <- function (author)
{
s <- sapply(rownames(installed.packages()),
packageDescription, fields = "Author")
names(grep(author, s, value = TRUE))
}
这是问题所在。在打开一个新的R会话并分配该函数时,对函数的总是的第一次调用返回一个空字符串的字符向量,它应该返回它的向量的正确长度。要显示此信息,请打开一个新的R会话,分配该功能,并使用您最喜欢的包作者的姓氏运行它。它应该首先返回一个空字符向量...
authoredPackages("Temple Lang")
# [1] "" "" "" ""
...然后再次执行并返回正确的结果......
authoredPackages("Temple Lang")
# [1] "jsonlite" "RCurl" "RJSONIO" "XML"
它始终只发生在新R会话的第一次呼叫中。为什么会发生这种情况,如何解决这个问题,以便第一次尝试使用该功能?
我的R --vanilla
会话信息:
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
更新:在打开R时,由于rownames(installed.packages())
包,似乎names
具有lme4
属性。不知道为什么,这是唯一的名字。它在第二次通话中消失的方式也很奇怪。
rownames(installed.packages())[228]
# ret0
# "lme4"
答案 0 :(得分:7)
奇怪,但似乎rownames(installed.packages())
在您第一次调用它时具有names
属性。
> str(rownames(installed.packages()))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages()))
chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" "colorspace" ...
抱歉,这让你回答这个问题。只要确保没有名字。这对您来说是一个问题,因为您依赖于sapply
的默认值USE.NAMES=TRUE
,但只会添加名称(如果它们尚未存在)。而且出于某些非常奇怪的原因,他们出现了。
authoredPackages <- function (author)
{
r <- setNames(rownames(installed.packages()), NULL)
s <- sapply(r, function(x) packageDescription(x)$Author)
names(grep(author, s, value = TRUE))
}
这是我的sessionInfo
(从R --vanilla
开始):
> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.1.1
我刚刚升级到R-3.1.2并再次尝试。我仍然得到相同的奇怪结果,如果我使用@MartinMorgan的建议使用noCache=TRUE
,我会始终如一地得到它们。
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> str(rownames(installed.packages(noCache=TRUE)))
Named chr [1:125] "bdsmatrix" "bitops" "blotter" "brew" "car" "changepoint" "chron" "colorout" ...
- attr(*, "names")= chr [1:125] "" "" "" "" ...
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] setwidth_1.0-3 colorout_1.0-1
loaded via a namespace (and not attached):
[1] tools_3.1.2