Last active
October 26, 2016 18:38
-
-
Save bhaskarvk/e29b679fecc29e036c216be9e5579057 to your computer and use it in GitHub Desktop.
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
R_BASEDIR=$HOME/Library/R/3.3 | |
# Create 3 dirs in $R_BASEDIR library, library.github, library.cran | |
mkdir -p R_LIBS=$R_BASEDIR/library $R_BASEDIR/library.github $R_BASEDIR/library.cran | |
# Directory to log all output | |
mkdir -p $HOME/Library/Logs/R | |
export R_LIBS=$R_BASEDIR/library:$R_BASEDIR/library.github:$R_BASEDIR/library.cran | |
# Bash functions to install R packages with ease. | |
# TODO color messages with crayon | |
mkdir -p $HOME/Library/Logs/R | |
# setup default options when starting r/R/Rscript | |
alias R='/usr/local/bin/R --quiet --no-save --no-restore' | |
alias r='/usr/local/bin/R --quiet --no-save --no-restore' | |
alias Rscript='/usr/local/bin/RScript --quiet --no-save --no-restore' | |
# Checks if a package is installed. | |
# Also reports the path where the package is installed. | |
rIsPkgInstalled() { | |
pkg=${1:-JUNK_PACKAGE} | |
ret=1 | |
dirs=($(Rscript -e 'cat(paste(.libPaths(),collapse=" "))')) | |
for dir in ${dirs[@]}; do | |
if [ -d "${dir}/${pkg}" ]; then | |
echo "$pkg Installed in dir: $dir" | |
ret=0 | |
fi | |
done | |
return $ret | |
} | |
# Install R packages from CRAN. | |
# Skip the ones that are already installed. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rInstPackages dplyr purrr tidyr | |
rInstPackages() { | |
packages=$(echo "$@" | sed -e 's/^/"/;s/ /", "/g;s/$/"/') | |
code=' | |
packages <- c('$packages'); | |
packages.all <- installed.packages(); | |
packages.i <- intersect(packages, packages.all); | |
packages.ni <- setdiff(packages, packages.all); | |
if(length(packages.i) > 0) { | |
print("------------------------------------------------------------") | |
print(sprintf("%s : Already Installed. Skipping.", | |
paste(packages.i, collapse=", "))); | |
print("------------------------------------------------------------") | |
}; | |
if (length(packages.ni) > 0) { | |
print(sprintf("Installing : %s",paste(packages.ni, collapse=", "))); | |
install.packages(packages.ni, lib="'$R_BASEDIR/library.cran'") | |
} | |
' | |
( | |
echo "Installing ${@}" | |
R --slave <<-EOT | |
${code} | |
EOT | |
echo | |
) 2>&1 | tee -a $HOME/Library/Logs/R/${1}-$(date +'%F_%H%M').txt | |
} | |
# Build R source packages from CRAN. | |
# Skip the ones that are already installed. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rInstSrcPackages dplyr purrr tidyr | |
rInstSrcPackages() { | |
packages=$(echo "$@" | sed -e 's/^/"/;s/ /", "/g;s/$/"/') | |
code=' | |
packages <- c('$packages'); | |
packages.all <- installed.packages(); | |
packages.i <- intersect(packages, packages.all); | |
packages.ni <- setdiff(packages, packages.all); | |
if(length(packages.i) > 0) { | |
print("------------------------------------------------------------") | |
print(sprintf("%s : Already Installed. Skipping.",paste(packages.i, collapse=", "))); | |
print("------------------------------------------------------------") | |
}; | |
if (length(packages.ni) > 0) { | |
print(sprintf("Installing : %s",paste(packages.ni, collapse=", "))); | |
install.packages(packages.ni, type="source", lib="'$R_BASEDIR/library.cran'") | |
} | |
' | |
( | |
echo "Installing ${@}" | |
R --slave <<-EOT | |
$code | |
EOT | |
echo | |
) 2>&1 | tee -a $HOME/Library/Logs/R/${1}-$(date +'%F_%H%M').txt | |
} | |
# Install R packages from CRAN. | |
# The ones that are already installed are reinstalled. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rInstPackagesForce dplyr purrr tidyr | |
rInstPackagesForce() { | |
packages=$(echo "$@" | sed -e "s/^/'/;s/ /', '/g;s/$/'/") | |
( | |
echo "Installing ${1}" | |
Rscript --slave -e "install.packages(c($packages), lib='$R_BASEDIR/library.cran')" | |
) 2>&1 | tee -a $HOME/Library/Logs/R/${1}-$(date +'%F_%H%M').txt | |
} | |
# Build R source packages from CRAN. | |
# The ones that are already installed are reinstalled. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rInstSrcPackagesForce dplyr purrr tidyr | |
rInstSrcPackagesForce() { | |
packages=$(echo "$@" | sed -e "s/^/'/;s/ /', '/g;s/$/'/") | |
( | |
echo "Installing ${1}" | |
Rscript --slave -e "install.packages(c($packages), type='source', lib='$R_BASEDIR/library.cran')" | |
) 2>&1 | tee -a $HOME/Library/Logs/R/${1}-$(date +'%F_%H%M').txt | |
} | |
# Install R packages from Github using devtools. | |
# The ones that are already installed are reinstalled. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rInstGithub dplyr purrr tidyr | |
rInstGithub() { | |
( | |
echo "Installing $1 from github" | |
Rscript --slave -e "devtools::install_github('$1', lib='$R_BASEDIR/library.github')" | |
) 2>&1 | tee -a $HOME/Library/Logs/R/${1/\//_}-$(date +'%F_%H%M').txt | |
} | |
# Helper functions for some famous github repo authors | |
rInstBob() { | |
rInstGithub "hrbrmstr/$1" | |
} | |
rInstKent() { | |
rInstGithub "timelyportfolio/$1" | |
} | |
rInstHadley() { | |
rInstGithub "hadley/$1" | |
} | |
rInstRstudio() { | |
rInstGithub "rstudio/$1" | |
} | |
rInstRopenGov() { | |
rInstGithub "ropengov/$1" | |
} | |
rInstOliver() { | |
rInstGithub "ironholds/$1" | |
} | |
rInstRopenSci() { | |
rInstGithub "ropensci/$1" | |
} | |
rInstRopenScilabs() { | |
rInstGithub "ropenscilabs/$1" | |
} | |
# Update R packages from CRAN. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rUpdatePackages | |
rUpdatePackages() { | |
( | |
echo "Updating R Packages from CRAN" | |
Rscript --slave -e "update.packages(ask=FALSE, lib.loc='$R_BASEDIR/library.cran', instlib='$R_BASEDIR/library.cran')" | |
) 2>&1 | tee -a $HOME/Library/Logs/R/cran_update-$(date +'%F_%H%M').txt | |
} | |
# Update R packages from Github. | |
# Log output under $HOME/Library/Logs/R | |
# e.g. rUpdateGithubPackages | |
rUpdateGithubPackages() { | |
( | |
echo "Updating R Packages from Github" | |
Rscript --slave -e "dtupdate::github_update(ask=FALSE, auto.install=TRUE)" | |
) 2>&1 | tee -a $HOME/Library/Logs/R/github_update-$(date +'%F_%H%M').txt | |
} | |
# vim:filetype=sh |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment