| title | Sleek R meta-packages: Pakages that handle package (downloading) | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| author | |||||||||||||||
| date | |||||||||||||||
| output |
|
(i) check if package installed
(ii) if not, install + load
As always found on StackOverflow: Elegant way to check for missing packages and install them?
Cool suggestion plus a cool R package name pacman by Tyler Rinker & Dason Kurkiewicz.
Check if pacman is installed, if not do; the rest are up to pacman
# Motivation of including extra code snippet:
# Annoying warning message spammer repo of the day:
# CRANextra = "http://www.stats.ox.ac.uk/pub/RWin"
options(repos = (CRAN = "https://cran.rstudio.com/"))
getOption("repos")
if (!require("pacman")) install.packages("pacman")
pacman::p_load(package1, package2, package_n)
{librarian} has proven to be a little less fool proof than {pacman}. Pacman never failed me thus far so sticking to this one :)
# Download librarian once
if (!require("librarian")) install.packages("librarian")
# Install everything else
library(librarian)
librarian::shelf()# Motivation of including extra code snippet:
# Annoying warning message spammer repo of the day:
# CRANextra = "http://www.stats.ox.ac.uk/pub/RWin"
options(repos = (CRAN = "https://cran.rstudio.com/"))
if (!require("devtools")) install.packages("devtools")
if (!require("parallel")) install.packages("parallel")
# Stack gem here: https://stackoverflow.com/questions/4090169/elegant-way-to-check-for-missing-packages-and-install-them
list.of.packages <- c("RcppEigen" ,
"Rcpp" ,
"MXM" ,
"Rfast" ,
"h2o" ,
"devtools" ,
"caret" ,
"mice" ,
"mice" ,
"randomForest",
"nnet" ,
"kernLab" ,
"glmnet" ,
"gbm" ,
"igraph" ,
"twidlr" ,
"corrr" ,
"forcats" ,
"tidygraph" )
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages,
Ncpus = ceiling(0.75 * parallel::detectCores())
)
library(devtools)
devtools::install_github(
repo = "tpq/exprso", # package path on GitHub
dependencies = TRUE ,
upgrade_dependencies = TRUE # updates any out of date dependencies
)
- Parallel package installation:
Funny enough, you can also use the
{parallel} R packageto avoid hardcoded numbers. SetNcpus = ceiling(0.75 * parallel::detectCores())For example, if your machine has 8 cores, theinstall.packages()function will use8 - ceiling(8 x 0.25)=8 - 2=6cores to install packages. - Set
options(install.packages.check.source = "no")??