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"
Odd, but seems like rownames(installed.packages())
has a names
attribute the first time you call it.
> 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" ...
Sorry, that left it to you to answer the question. Just make sure there are no names. This is a problem for you because you're relying on sapply
's default of USE.NAMES=TRUE
, but that only adds names if they're not already present. And they're present for some really weird reason.
authoredPackages <- function (author)
{
r <- setNames(rownames(installed.packages()), NULL)
s <- sapply(r, function(x) packageDescription(x)$Author)
names(grep(author, s, value = TRUE))
}
Here's my sessionInfo
(from starting with 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
I just upgraded to R-3.1.2 and tried again. I still get the same odd results, and I get them consistently if I use @MartinMorgan's suggestion to use 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With