I have the following function for personal use. It takes the author’s name to find out if I have any of my packages on my machine.
authoredPackages <- function (author) { s <- sapply(rownames(installed.packages()), packageDescription, fields = "Author") names(grep(author, s, value = TRUE)) }
Here is the problem. When you open a new R session and assign a function, the first function call always returns a character vector of empty strings of the correct length of the vector that it should return. To show this, open a new R session, assign a function, and run it with your favorite surname of the package author. It must first return an empty character vector ...
authoredPackages("Temple Lang") # [1] "" "" "" ""
... and then repeat it and it will return the correct result ...
authoredPackages("Temple Lang") # [1] "jsonlite" "RCurl" "RJSONIO" "XML"
This always happens only on the first call in a new R session. Why is this happening and how can I fix it, so the function always works on the first try?
My R --vanilla session information:
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:. When opening R, it seems that rownames(installed.packages()) has the names attribute due to the lme4 package. I don’t know why, and this is the only name. It is also very strange how it disappears on the second call.
rownames(installed.packages())[228] # ret0 # "lme4"
r
Rich scriven
source share