[R] Project local libraries (reproducible research)
Hadley Wickham
hadley at rice.edu
Mon Dec 5 14:30:53 CET 2011
On Sat, Dec 3, 2011 at 11:16 AM, Jim Lemon <jim at bitwrit.com.au> wrote:
> On 12/03/2011 06:04 AM, Hadley Wickham wrote:
>>
>> Hi all,
>>
>> I was wondering if any one had scripts that they could share for
>> capturing the current version of R packages used for a project. I'm
>> interested in creating a project local library so that you're safe if
>> someone (e.g. the ggplot2 author) updates a package you're relying on
>> and breaks your code. I could fairly easily hack together, but I was
>> wondering if any one had any neat scripts they'd care to share.
>>
> Hi Hadley,
> This makes quite a few assumptions, but I would build a list of packages
> called in R scripts:
>
> # get all calls to library or require in a subdirectory of scripts
> packagestrings<-c(system("grep library *.R",intern=TRUE),
> system("grep require *.R",intern=TRUE))
> # get the unique package names
> packagenames<-unique(sapply(strsplit(packagestrings,"[()]"),"[",2))
> # get the information on all installed packages in the system
> installedpackages<-installed.packages()
> # get the indices of the packages used in the subdirectory
> whichpackages<-which(match(rownames(installedpackages),packagenames,0)>0)
> # get the version information for each used package
> installedpackages[whichpackages,"Built"]
Thanks Jim. I was thinking something more like:
rec <- dir(file.path(Sys.getenv("R_HOME"), "library"))
pkgs <- search()[grepl("package:", search(), fixed = T)]
pkgs <- gsub("package:", "", pkgs, fixed = TRUE)
pkgs <- setdiff(pkgs, rec)
i.e. work from the search path (once you've run your code). Then the
next step would be find all dependencies of the packages, then copy
them into the current working directory, and give a line to set
.libPaths() correctly.
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
More information about the R-help
mailing list