Skip to content

Instantly share code, notes, and snippets.

@andrewmoles2
Created April 27, 2022 10:18
Show Gist options
  • Save andrewmoles2/e52307e0296955067e50a6651aa935d0 to your computer and use it in GitHub Desktop.
Save andrewmoles2/e52307e0296955067e50a6651aa935d0 to your computer and use it in GitHub Desktop.
# function to check install and load packages
load_library <- function(pkg) {
# check if package is installed
new_package <- pkg[!(pkg %in% installed.packages()[, "Package"])]
# install any new packages
if (length(new_package))
install.packages(new_package, dependencies = TRUE, Ncpus = 6)
# sapply to loop through and load packages
invisible(sapply(pkg, library, character.only = TRUE))
# give message confirming load
message("The following packages are now loaded")
print(names(sessionInfo()$otherPkgs))
}
# use test
packages <- c('tidyverse', 'report', 'tidytext', 'data.table')
load_library(packages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment