Skip to content

Instantly share code, notes, and snippets.

@ArnauMontagud
Last active August 5, 2021 11:03
Show Gist options
  • Save ArnauMontagud/a2d3270c9e670cb6a2bac76310e93278 to your computer and use it in GitHub Desktop.
Save ArnauMontagud/a2d3270c9e670cb6a2bac76310e93278 to your computer and use it in GitHub Desktop.
[R Check for missing packages and install them] Place at the beginning of the R script to check for missing packages and install them (and only in option2 load them) #R
# from: https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
# This checks for missing packages and installs them. You will need to load them manually
list.of.packages <- c("ggplot2", "magrittr")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
# library(ggplot2)
# This checks for missing packages, installs them and loads them.
if (!require("pacman")) install.packages("pacman")
list.of.packages <- c("ggplot2", "magrittr")
pacman::p_load(list.of.packages, character.only = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment