Names of R's available packages
A better way than scraping a web page to get the names of packages is to use the available.packages()
function and process those results. available.packages()
returns a matrix contains details of all packages available (but is filtered by default — see the Details section of ?available.packages
for more).
pkgs <- available.packages(filters = "duplicates")
nameCount <- unname(nchar(pkgs[, "Package"]))
table(nameCount)
> table(nameCount)
nameCount
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
32 311 374 360 434 445 368 277 199 132 99 56 56 43 22 19 18 2 12 8
22 24 25 31
5 2 1 1
Using nameCount
we can select packages with names containing any number of characters without needing to resort to regexp etc:
> unname(pkgs[which(nameCount == 2), "Package"])
[1] "BB" "bs" "ca" "cg" "dr" "ez" "FD" "ff" "HH" "HI" "iv" "JM" "ks" "M3" "mi"
[16] "np" "oc" "oz" "PK" "PP" "qp" "QT" "RC" "rv" "Rz" "sm" "sn" "sp" "st" "SV"
[31] "tm" "wq"
What constitutes a good package name according to CRAN?
Writing R Extensions only provides the following constraints:
The mandatory ‘Package’ field gives the name of the package. This should contain only (ASCII) letters, numbers and dot, have at least two characters and start with a letter and not end in a dot.
Note that the underscore character, _
, is not allowed.
You might start your research by examining the listing here - in particular, few package names include the dot char, .
.
Also, take look at this SO question for some helpful code. What's more, @agstudy provided a link to Hadley Wickham's tips on his favorite package naming conventions here.
By the way, in case you're planning to submit the package to CRAN,
the CRAN team might suggest a name change if it's not appropriate.
Why does rownames(installed.packages()) have a names attribute?
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
How can I extract the names of all package authors from CRAN
Taken from reverse_dependencies_with_maintainers
, which was available at one point on the R developer site (I don't see it there now):
description <- sprintf("%s/web/packages/packages.rds",
getOption("repos")["CRAN"])
con <- if(substring(description, 1L, 7L) == "file://") {
file(description, "rb")
} else {
url(description, "rb")
}
db <- as.data.frame(readRDS(gzcon(con)),stringsAsFactors=FALSE)
close(con)
rownames(db) <- NULL
head(db$Author)
head(db$"Authors@R")
Where Authors@R
exists it might be parseable into something better using dget()
getAuthor <- function(x){
if(is.na(x)) return(NA)
a <- textConnection(x)
on.exit(close(a))
dget(a)
}
authors <- lapply(db$"Authors@R", getAuthor)
head(authors)
[[1]]
[1] NA
[[2]]
[1] "Gaurav Sood <gsood07@gmail.com> [aut, cre]"
[[3]]
[1] "Csillery Katalin <kati.csillery@gmail.com> [aut]"
[2] "Lemaire Louisiane [aut]"
[3] "Francois Olivier [aut]"
[4] "Blum Michael <michael.blum@imag.fr> [aut, cre]"
[[4]]
[1] NA
[[5]]
[1] "Csillery Katalin <kati.csillery@gmail.com> [aut]"
[2] "Lemaire Louisiane [aut]"
[3] "Francois Olivier [aut]"
[4] "Blum Michael <michael.blum@imag.fr> [aut, cre]"
[[6]]
[1] NA
Elegant way to load a string list of packages in R
Here is one option
Pkgs <- c('caret','readxl')
lib <- .libPaths()[1]
i1 <- !(Pkgs %in% row.names(installed.packages()))
if(any(i1)) {
install.packages(Pkgs[i1], dependencies = TRUE, lib = lib)
}
Show names of everything in a package
ls("package:foreach", all.names=TRUE)
only shows what's attached to the search path, which only includes the objects exported from the namespace. Use ls
on the results of getNamespace
instead:
ls(getNamespace("foreach"), all.names=TRUE)
How to find out which package version is loaded in R?
You can use sessionInfo()
to accomplish that.
> sessionInfo()
R version 2.15.0 (2012-03-30)
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=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] graphics grDevices utils datasets stats grid methods base
other attached packages:
[1] ggplot2_0.9.0 reshape2_1.2.1 plyr_1.7.1
loaded via a namespace (and not attached):
[1] colorspace_1.1-1 dichromat_1.2-4 digest_0.5.2 MASS_7.3-18 memoise_0.1 munsell_0.3
[7] proto_0.3-9.2 RColorBrewer_1.0-5 scales_0.2.0 stringr_0.6
>
However, as per comments and the answer below, there are better options
> packageVersion("snow")
[1] ‘0.3.9’
Or:
"Rmpi" %in% loadedNamespaces()
Where are the installed R packages?
You can view the location by running the following:
.libPaths()
#[1] "C:/Users/ujjwal/Documents/R/win-library/3.1" "C:/Program Files/R/R-3.1.1/library"
R packages are installed into libraries, which are directories in the file system containing a subdirectory for each package installed there.
R comes with a single library, R_HOME/library
which is the value of the R object .Library
containing the standard and recommended packages. At the lowest level .libPaths()
can be used to add paths to the collection of libraries or to report the current collection.
R will automatically make use of a site-specific library R_HOME/site-library
if this exists. This location can be overridden by setting .Library.site
in R_HOME/etc/Rprofile.site
. For more details see here.
Related Topics
Knitr (R) - How Not to Embed Images in the HTML File
Replace All Na with False in Selected Columns in R
How to Change Fontface (Bold/Italics) for a Cell in a Kable Table in Rmarkdown
How to Append Data from a Data Frame in R to an Excel Sheet That Already Exists
Intersect All Possible Combinations of List Elements
Multiple Histograms in Ggplot2
Format Date to Year-Month in R
Interpolate Zoo Object with Missing Dates
How to Close Unused Connections After Read_HTML in R
How to Rotate Legend Symbols in Ggplot2
Knitr: Getting a Parse_All Error in R When Converting Rmd File into HTML
Regular Analysis Over Irregular Time Series
How to Get Last Subelement of Every Element of a List
Grouped Barplot with Cut Y Axis