I have the following function for personal use. It takes in an author's name to see if I have any of their packages on my machine.
authoredPackages <- function (author)
{
s <- sapply(rownames(installed.packages()),
packageDescription, fields = "Author")
names(grep(author, s, value = TRUE))
}
Here's the problem. Upon opening a new R session and assigning the function, the first call to the function always returns a character vector of empty strings the correct length of the vector it's supposed to return. To show this, open a new R session, assign the function, and run it with your favorite package author's surname. It should first return an empty character vector ...
authoredPackages("Temple Lang")
# [1] "" "" "" ""
... and then do it again and it returns the correct result ...
authoredPackages("Temple Lang")
# [1] "jsonlite" "RCurl" "RJSONIO" "XML"
It always only happens on the first call in a new R session. Why does this happen, and how can I fix it so the function always works on the first try?
My R --vanilla
session info:
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
Update: Upon opening R, is seems rownames(installed.packages())
has a names
attribute because of the lme4
package. Not sure why, and it's the only name. It's also very strange how it disappears on the second call.
rownames(installed.packages())[228]
# ret0
# "lme4"