Skip to content

Instantly share code, notes, and snippets.

@SimonGoring
Last active January 2, 2019 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimonGoring/fe3b0bf6b4cb9b8f0a2aa6cdce10bb29 to your computer and use it in GitHub Desktop.
Save SimonGoring/fe3b0bf6b4cb9b8f0a2aa6cdce10bb29 to your computer and use it in GitHub Desktop.
Recursively searches R and Rmd files in a directory to install all required packages using R.
#!/bin/bash
# This scrips is described in more detail at http://www.goring.org/resources/regex_bash_libraries.html
rinstall=0
while getopts "i" OPTION
do
case $OPTION in
i)
echo Running installLib with the option to install packages.
rinstall=1
;;
esac
done
rpath=$(Rscript -e "cat(.libPaths())")
library=$(cat $(find . -type f \( -name \*.R -o -name \*.Rmd \)) | sed -n 's/^library[(]\(.*\)[)]/\1/p' | tr "," "\n" | tr -d "[\"\\']" | sed "s/\(quietly\|verbose\)\s*=\s*\(\(TRUE\)\|\(FALSE\)\)/ /g")
library+=$(cat $(find . -type f \( -name \*.R -o -name \*.Rmd \)) | sed -n 's/^require[(]\(.*\)[)]/ \1/p' | tr "," "\n" | tr -d "[\"\\']" | sed "s/\(quietly\|verbose\)\s*=\s*\(\(TRUE\)\|\(FALSE\)\)/ /g")
library+=$(cat $(find . -type f \( -name \*.R -o -name \*.Rmd \)) | sed -n 's/^.*\@import\(From\)\?\s\([a-zA-Z]*\)\s.*/ \2/p')
library+=$(cat $(find . -type f \( -name \*.R -o -name \*.Rmd \)) | perl -e 's/(.*?)([[:alnum:]]+)(:{2,3})(.*?)|./ \2/g' | sed '/^\s*$/d')
installs=$(tr ' ' '\n' <<< "${library[@]}" | sort -u | tr '\n' ' ')
for onePkg in $installs; do
install=0
for paths in $rpath; do
test -d "$paths/$onePkg" && install=1
done
test $install -eq 0 && printf " The package %s hasn\'t been installed.\n" $onePkg
test $install -eq 0 && test $rinstall -eq 1 && printf " * Will now install the package.\n" && Rscript -e "install.packages (\"$onePkg\", repos=\"http://cran.r-project.org/\")"
done

Copyright (c) 2018 Simon Goring

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@SimonGoring
Copy link
Author

This script is described in more detail on my webpage: http://www.goring.org/resources/regex_bash_libraries.html

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