Skip to content

Instantly share code, notes, and snippets.

@tonyelhabr
Last active May 5, 2020 23:52
Show Gist options
  • Save tonyelhabr/f7f4f3b4725c7e3a6a274018d008ffe9 to your computer and use it in GitHub Desktop.
Save tonyelhabr/f7f4f3b4725c7e3a6a274018d008ffe9 to your computer and use it in GitHub Desktop.
Installing R package on a new desktop/laptop.
dir_pkgs_rds <- 'c:/users/aelhabr/desktop'
r_version_prev <- '3.5'
r_version_new <- '4.0'
path_rds_prev <- file.path(dir_pkgs_rds, sprintf('pkgs_%s.rds', r_version_prev))
dir_pkgs_installed <- 'c:/users/aelhabr/documents/r/win-library'
# ----
# + Open RStudio.
# + Run the top-level lines.
# + Save installed packages. (See below.)
pkgs <- installed.packages()
saveRDS(pkgs, path_rds_prev)
# + Close RStudio.
# + Run installr::installr() in RGui.
# + Re-install rtools outside of RGui and RStudio.
# + Re-open RStudio.
# + Install some "core" packages manually, before installing packages saved in `path_rds_prev`. (See below.) This is probably unnecessary, but I just want to make sure that they are installed first since many other packages seem to depend on them.
pkgs_manual <- c('askpass', 'sys', 'jsonlite', 'mime', 'httr', 'xml2', 'openssl', 'R6', 'rlang')
for(pkg in pkgs_manual) {
install.packages(pkg)
}
# + Run the top-level lines again.
pkgs <- readRDS(path_rds_prev)
install.packages(pkgs[, 1]) # Wait a long time.
# The rest of this is optional
# + After installing, look for packages that weren't properly installed (probably because they weren't originally from CRAN.)
list_pkgs_dirs <- function(r_version) {
list.dirs(
file.path(dir_pkgs_installed, r_version),
recursive = FALSE,
full.names = FALSE
)
}
pkgs_prev <- list_pkgs_dirs(r_version_prev)
pkgs_new <- list_pkgs_dirs(r_version_new)
pkgs_diff <- setdiff(pkgs_prev, pkgs_new)
pkgs_diff
pkgs_diff
# pkgs_diff_exclude <-
# c('BiocGenerics', 'BiocInstaller', 'BiocVersion', 'cypress', 'espn2', 'g2r', 'tedata', 'teml', 'teplot', 'teproj', 'testyle', 'tetidy') # Packages that I don't want to try to re-install
# pkgs_github <- setdiff(pkgs_diff, pkgs_diff_exclude)
# githubinstall::gh_install_packages(pkgs_github) # This will prompt very every package that there is some ambiguity about, so it can be sort of annoying.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment