Skip to content

Instantly share code, notes, and snippets.

@cgpu
Last active January 15, 2019 14:57
Show Gist options
  • Select an option

  • Save cgpu/7348dd08604e44e83128bb294ee4adc8 to your computer and use it in GitHub Desktop.

Select an option

Save cgpu/7348dd08604e44e83128bb294ee4adc8 to your computer and use it in GitHub Desktop.
Elegant R package (down)loading with the `pacman` R package
title Sleek R meta-packages: Pakages that handle package (downloading)
author
date
output
html_document
toc toc_depth toc_float number_sections theme highlight
true
3
true
true
united
tango

Elegant way to:

(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.

pacman R package: effortless R package (down)loading

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)

EDIT:

{librarian} has proven to be a little less fool proof than {pacman}. Pacman never failed me thus far so sticking to this one :)

librarian - One-step packages from CRAN, GitHub, and Bioconductor by @eco_desi

# Download librarian once
if (!require("librarian")) install.packages("librarian")

# Install everything else
library(librarian)
librarian::shelf()

Batch installation of packages:

# 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
)

General tips and parameter settings for the install.packages() function

  • Parallel package installation: Funny enough, you can also use the {parallel} R package to avoid hardcoded numbers. Set Ncpus = ceiling(0.75 * parallel::detectCores()) For example, if your machine has 8 cores, the install.packages() function will use 8 - ceiling(8 x 0.25) = 8 - 2 = 6 cores to install packages.
  • Set options(install.packages.check.source = "no") ??

Resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment