Created
February 18, 2021 15:30
-
-
Save alanhoyle/a0993c512ef0db514e987811bcca4276 to your computer and use it in GitHub Desktop.
Install packages or die function for R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 "\ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by an answer from this StackOverflow question