From hadley at rice.edu  Wed Aug 25 14:32:33 2010
From: hadley at rice.edu (Hadley Wickham)
Date: Wed, 25 Aug 2010 07:32:33 -0500
Subject: [Rd] require is to suggests as what is to imports?
In-Reply-To: <alpine.LFD.2.00.1008250749080.28875@gannet.stats.ox.ac.uk>
References: <AANLkTi=EOFdYKP0JUZLCz8r7KRJ1PaYFGQgresAQJ6r1@mail.gmail.com>
	<19572.12499.732450.953347@max.nulle.part>
	<alpine.LFD.2.00.1008250749080.28875@gannet.stats.ox.ac.uk>
Message-ID: <AANLkTikXvvggGKGPYsH-rEUUzPDgZFuLFiiVgpK2Adf0@mail.gmail.com>

> But thereby clobber your users with the run-time cost of
> installed.packages() (which can take several minutes on some Windows
> systems, and just took ca 12secs on my fastest Linux server with 3000
> packages installed). ?If you want to take this route (is a package
> installed?), see the 'Note' on ?installed.packages for better alternatives.

On that note, I wrote a version of installed.packages() which runs
quite a bit faster on my computer:

installed_packages <- function() {
  paths <- unlist(lapply(.libPaths(), dir, full.names = TRUE))
  desc <- file.path(paths, "DESCRIPTION")
  desc <- desc[file.exists(desc)]

  dcf <- lapply(desc, read.dcf, fields = c("Package", "Title", "Version"))
  packages <- as.data.frame(do.call("rbind", dcf), stringsAsFactors = FALSE)

  packages$status <- ifelse(packages$Package %in% .packages(),
    "loaded", "installed")
  class(packages) <- c("packages", class(packages))
  packages[order(packages$Package), ]
}

It probably runs faster because I've eliminated some features, and
it's probably not worth spending much time optimising such a rarely
used function, but there it is for what it's worth.

Hadley

-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



[Rd] require is to suggests as what is to imports?
Hadley Wickham