Skip to main content

Just wondering if anyone has some pointers or ideas on how you install R packages for lab use? Most of of what I see describes installing via the R console

I’m also wondering if there is a way to install directly from the repositories

What I really want to do is script it so that I can just deploy using Jamf Pro

Here's one I used a while back.

#!/usr/local/bin/Rscript

args <- commandArgs(TRUE)


if(length(args[]) < 4) {
    print("Must include at least one package for installation")
    q()
}


#First three arguements are reserved for the JSS.  Get rid of them...
args <- args[-1]
args <- args[-1]
args <- args[-1]




for(currentPackage in args) {
    message("Installing: ", currentPackage )
    if(!require(currentPackage,character.only = TRUE)){
        message("Installing: ", currentPackage )
        install.packages(currentPackage , dependencies = TRUE, repos='http://cran.us.r-project.org')
    }else{
        message("Package ", currentPackage, " is already installed")
    }
}

q()

Thanks @Reno (Brad) - that helps massively. I have somewhere around 300 packages that were requested so I'll do some reading and see if I can add something like a here doc in Bash to Rscript.

Thanks for lifting the lid - much appreciated

Regards,
David


This is what I came up with:

 

#!/bin/bash # Install_R_Package.bash # 2020-06-22 David London # Credit and Inspiration - Brad Reno https://www.jamf.com/jamf-nation/discussions/35969/installing-r-packages-in-lab # path to Rscript script that installs the packages script="/tmp/Install_R_Package.R" echo "creating /tmp/Install_R_Package.R" # Create the Rscript file echo "#!/usr/local/bin/Rscript" > "$script" echo "# Install_R_Package.R" >> "$script" echo "# https://www.jamf.com/jamf-nation/discussions/35969/installing-r-packages-in-lab" >> "$script" echo >> "$script" echo "args <- commandArgs(TRUE)" >> "$script" echo >> "$script" echo "if(length(args[]) < 1) {" >> "$script" echo " print(\\"Must include at least one package for installation\\")" >> "$script" echo " q()" >> "$script" echo "}" >> "$script" echo >> "$script" echo "for(currentPackage in args) {" >> "$script" echo " message(\\"Installing: \\", currentPackage )" >> "$script" echo " if(!require(currentPackage,character.only = TRUE)){" >> "$script" echo " message(\\"Installing: \\", currentPackage )" >> "$script" echo " install.packages(currentPackage , dependencies = TRUE, repos='http://cran.us.r-project.org')" >> "$script" echo " }else{" >> "$script" echo " message(\\"Package \\", currentPackage, \\" is already installed\\")" >> "$script" echo " }" >> "$script" echo "}" >> "$script" echo >> "$script" echo "q()" >> "$script" echo "making /tmp/Install_R_Package.R executable" chmod +x "$script" # The list of packages required packages=$(cat <<EOF abind acepack addinslist AER akima anytime ape aplpack arm askpass assertthat astro audio backports base base64enc beepr BH bindr bindrcpp bitops boot brew broom BWStest callr car carData caTools cellranger checkmate chron class classInt cli clipr clisymbols cluster coda codetools coin colorspace colourpicker combinat commonmark compiler crayon crosstalk curl data.table DataExplorer datasets DBI dbplyr Dcchoice DEoptimR desc DescTools devtools digest dplyr DT e1071 effects effsize ellipsis emmeans EMT estimability evaluate expm fansi fastICA FNN forcats foreign formatR Formula fs gdata gdistance generics geoR geosphere ggfittext ggmap ggplot2 ggThemeAssist gh git2r glue gmp googlePolylines googleway graphics grDevices grid gridExtra gstat gtable gtools gvlma haven hexbin highr Hmisc hms htmlTable htmltools htmlwidgets httpuv httr igraph ImportExport ini inspectdf intervals ipdw jomo jpeg jqr jsonlite KernSmooth klaR knitr kSamples labeling labelled later lattice latticeExtra lazyeval lctools libcoin lme4 lmtest lsmeans magrittr manipulateWidget maptools markdown MASS Matrix matrixcalc MatrixModels matrixStats memoise methods mgcv mi mice mime miniUI minqa mitml mitools modelr modeltools multcomp multcompView munsell mvtnorm networkD3 nlme nloptr nnet nortest numDeriv nycflights13 openssl OpenStreetMap openxlsx ordinal pan parallel pbkrtest PBSmapping pillar pkgbuild pkgconfig pkgload plogr plotrix plyr PMCMR PMCMRplus png praise prettymapr prettyunits processx progress proj4 promises proto ps pscl purrr quantreg questionr R6 RandomFields RandomFieldsUtils rappdirs raster rcmdcheck Rcmdr RcmdrMisc RColorBrewer rcompanion Rcpp RcppEigen readr readstata13 readxl relimp rematch remotes reprex reshape reshape2 rgdal rgeos rgl RgoogleMaps rgr rio rJava rjson rlang rmarkdown Rmisc Rmpfr robustbase RODBC roxygen2 rpanel rpart rprojroot rstudioapi rvest sandwich scales selectr sem sessioninfo shades shiny shinyjs soilphysics soiltexture soilwater sourcetools sp spacetime SparseM spatial spData splancs splines stargazer stats stats4 stringi stringr SuppDists survey survival sys tcltk tcltk2 testthat TH.data tibble tidyr tidyselect tidyverse tinytex tkrplot tools translations ucminf usethis utf8 utils vctrs vegan viridis viridisLite webshot weights whisker withr wordcloud2 xfun xlsx xlsxjars xml2 xopen xtable xts yaml zeallot zip zoo EOF ) # echo $packages # Install the packages for i in $packages do echo "Attempting to install" $i /usr/local/bin/Rscript $script $i done exit 0