简体   繁体   中英

updating package in R: `update.packages` vs. `install.packages`

I've tried to load the party library and got the following error:

 Loading required package: zoo
 Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
   namespace ‘lattice’ 0.20-24 is already loaded, but >= 0.20.27 is required
 Error: package ‘zoo’ could not be loaded

So I decided to update all packages within the same session ( detach all packages while working in R ), including lattice , hoping that zoo and then party would then load correctly once lattice is updated:

 pkgs <- names( sessionInfo()$otherPkgs )
 pkgs <- paste('package:', pkgs, sep = "")
 lapply( pkgs , detach, character.only = TRUE, unload = TRUE)
 update.packages(checkBuilt=TRUE, ask=FALSE,
                 repos="http://r-forge.r-project.org",
                 oldPkgs=c("lattice","zoo","party")
 )

It didn't work (within the same session and after re-starting without preloading .RData ):

 > library(party)
 Loading required package: zoo
  Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : 
   namespace ‘lattice’ 0.20-24 is already loaded, but >= 0.20.27 is required
   Error: package ‘zoo’ could not be loaded

According to How to update R2jags in R? it's best to simply run install.packages on those packages I want to update, then restart. And indeed it did the trick.

So here's the question: when is update.packages called for, given that updating within a running session is fragile to say the least, and install.package will do the trick at the cost of restarting the session? What bit of R package management voodoo am I missing? Thanks.

Dirk offers a more general strategy to avoid this issue. However, if you are in an interactive session that you don't feel like rebooting, and you want to unload a package that needs updating (which neither detach(.) -ing or update.packages(.) -ing effectively accomplishes), then there is a function, unloadNamespace that usually works for me. There are warnings in its help page that say it's not entirely safe, but I have not had difficulties. Try:

unloadNamespace("lattice")   # or lapply()-ing as you attempted with `detach`
update.packages("lattice")
require(lattice)  # or library()

This is yet another reason why I prefer to launch both the "install" and "update" operations outside of my current, working R session.

By using the command-line, I get fresh R sessions without loaded packages, and the issue you experienced here does not arise. And as a shortcut, I define scripts update.r and install.r using littler (and included in the examples/ directory of that package) but you can of course do the same via Rscript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM