Why do rownames (installed.packages ()) have a name attribute?

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" 
+7
r
source share
1 answer

Odd, but it looks like rownames(installed.packages()) has a names attribute on the first call.

 > 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, this left you an answer to the question. Just make sure there are no names. This is a problem for you because you rely on sapply by default USE.NAMES=TRUE , but that only adds names if they are not already present. And they are present for some really strange 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 is my sessionInfo (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 sequentially if I use @MartinMorgan 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 
+7
source share

All Articles