2

I'm maintaining an openCPU (R API) instance with frequent package updates. OpenCPU (sensibly) separates its own core packages into different folders, so that they aren't accidentally broken out of the lockstep with the installed version.

However, this can lead to duplicated packages being installed in the user folder which, then again, leads to errors when the openCPU API tries to unload and reattach the package to get the newer version. I frequently cause these sorts of problems when trying to update packages.

I usually check for them using this snippet.

ocpubasics <- rownames(installed.packages(lib.loc ="/usr/lib/opencpu/library"))
userpkgs <- rownames(installed.packages(lib.loc="/usr/local/lib/R/site-library"))
(dupe_pkgs <- userpkgs[ userpkgs %in% ocpubasics])
remove.packages(dupe_pkgs, lib="/usr/local/lib/R/site-library")

However, this doesn't catch all cases (because they are five library paths) and also removes duplicates that aren't mismatched for version (which don't really hurt and are sometimes necessary for a package to be installed). So, I'm wondering if someone has written a function which, given a vector of library paths, checks whether any package has a mismatched version installed in a different library path.

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Ruben
  • 3,452
  • 31
  • 47

1 Answers1

2

I ended up writing the following code; maybe it's useful to others.

An old package could hide in any library

.libPaths(c( "/usr/local/lib/opencpu/site-library", 
                         "/usr/local/lib/R/site-library",
                         "/usr/lib/R/site-library",
                         "/usr/lib/R/library",   
                        "/usr/lib/opencpu/library"  ))

Get a list of duplicated packages

library(tidyverse)
pkgs <- installed.packages()
pkgs <- as.data.frame(pkgs)

dupes <- pkgs %>% select(Package, Version, LibPath) %>% 
    group_by(Package) %>% 
    filter(n_distinct(Version, na.rm = TRUE) > 1) 

Check which version is installed in which library

dupes %>% 
    spread(LibPath, Version) %>% 
    knitr::kable()

Remove any duplicates with older versions

dupes %>% 
    group_by(Package) %>% 
    arrange(desc(Version)) %>% 
    filter(Version != first(Version)) %>% 
    purrr::pmap(~ remove.packages(..1, ..3))
Ruben
  • 3,452
  • 31
  • 47