Skip to content

Instantly share code, notes, and snippets.

@HenrikBengtsson
Last active December 13, 2015 23:21
Show Gist options
  • Save HenrikBengtsson/7881a291cdd61708ba51 to your computer and use it in GitHub Desktop.
Save HenrikBengtsson/7881a291cdd61708ba51 to your computer and use it in GitHub Desktop.
Identifying R packages that fail to load and try to re-install them
## Identify installed packages that fail to load
rscript <- function(...) {
system2(file.path(R.home("bin"), "Rscript"), ...)
}
works <- function(pkg) {
## Need to call in separate R session to not use up all DLLs
rcmd <- sprintf("cat(isTRUE(requireNamespace('%s')))", pkg)
as.logical(rscript(args=c("-e", dQuote(rcmd)), stdout=TRUE))
}
pkgs <- installed.packages()
pkgs <- pkgs[(pkgs[,"NeedsCompilation"] == "yes"),"Package"]
pkgs <- sort(unique(pkgs))
ok <- sapply(pkgs, FUN=works)
bad <- pkgs[!ok]
## Try to reinstall
install.packages(bad)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment