Skip to content

Instantly share code, notes, and snippets.

@Godrigos
Created January 21, 2022 14:22
Show Gist options
  • Save Godrigos/c6faaa0c2d2541dc8eb0e6b0d1b2b963 to your computer and use it in GitHub Desktop.
Save Godrigos/c6faaa0c2d2541dc8eb0e6b0d1b2b963 to your computer and use it in GitHub Desktop.
Check if required R packages are installed, and load them.
## If a package is installed, it will be loaded. If any
## are not, the missing package(s) will be installed
## from CRAN and then loaded.
## First specify the packages of interest
packages = c("tidyverse", "geomorph",
"phytools", "viridis")
## Now load or install & load all
package.check <- lapply(
packages,
FUN = function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, dependencies = TRUE)
library(x, character.only = TRUE)
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment