Skip to content

Instantly share code, notes, and snippets.

@alanhoyle
Created February 18, 2021 15:30
Show Gist options
  • Save alanhoyle/a0993c512ef0db514e987811bcca4276 to your computer and use it in GitHub Desktop.
Save alanhoyle/a0993c512ef0db514e987811bcca4276 to your computer and use it in GitHub Desktop.
Install packages or die function for R
# This function attempts to install a provided list of packages.
# If one of the requested installs fails, it throws an R Error. By default
# This is a useful function to use in a Dockerfile or command line.
# You can put run this at a command line with:
R -e " \
install_packages_or_die <- function (pkgs, repos='http://cran.rstudio.com/') { \
for (l in pkgs) { install.packages(l, dependencies=TRUE, repos=repos); \
if ( ! library(l, character.only=TRUE, logical.return=TRUE) ) { \
stop ('ERROR: failed installing requested package \'',l,'\'') } } } ; \
install_packages_or_die (pkgs= c('mime')); "
# For Dockerfile, use:
# RUN R -e "\
@alanhoyle
Copy link
Author

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