8 comments on “My favorite R packages (installed with one command)

  1. “I’ve learned something new today”, thank you for your list 🙂

    It should be a good habit for people (including me) to start compiling such a list and sharing it also.


  2. That’s very inefficient. `install.packages()` takes a vector of package names and you can check if all packages are installed or not in a single sweep.

    One way to wrap this would be:

    installMyPackages <- function(packages, lib, repos = getOption("repos"),
    update = TRUE, load = TRUE,
    quietly = TRUE) {
    packages <- as.character(packages)
    lib <- .libPaths()[1]
    if(update) {
    update.packages(ask = FALSE, repos = repos)
    instPack <- installed.packages()
    want <- !(packages %in% instPack[,"Package"])
    install.packages(packages[want], dependencies = TRUE, lib = lib)
    if(load) {
    tmp <- sapply(packages, require, character.only = TRUE,
    quietly = quietly)
    writeLines(strwrap(paste("Attempted to install packages:",
    paste(packages, collapse = ", "))))

    installMyPackages(p, repos = repositories)

    That way you only incur one update and install overhead.

    • Gavin, this looks very useful. I know I’m going to be getting a new computer in the coming year, so I will give this a try.

      Question — in the function installMyPackages(p, repos = repositories), I’d like to populate “p” automatically, using p installed.packages()[,1]

      ** any suggestions on how to dispense with the duplicate names? Looks to me like install.packages and update.packages only want the names in quotes.

      Jesse, thanks for this idea.

      regards, Paul

      • What you are seeing is a *named* vector in R parlance – the name just happen to be the same as the data in the character vector, there is no duplication of data. This is a side effect of sub-setting and dropping dimensions. The matrix returned by `installed.packages()` has row names and these become the names of the character vector when you subset. You can, and R will, ignore them and you can safely store the result and pass it to my or Jesse’s function as is. If you want to get rid of the names, `unname(installed.packages()[,1])` will do this for you.

  3. Pingback: Instalando muitos pacotes no R « EstatĂ­stica Brasil

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s