Quick Script to Install an R Package from the Command Line

I wrote a really quick script to install R packages from the command line that I thought I’d share.

It doesn’t really do a great deal, but you can use it to install one package at a time.

Save the below as rpkginstall and make sure it’s executable with chmod + x rpkginstall.

Then you can install a package like this example, which would install dplyr, ./rpkginstall dplyr.

#!/usr/bin/env bash


function help {
  echo "Usage: $(basename $0) <package name>"
  exit 1

[ -z ${pkg} ] && help

REXEC=$(which R)

if [ -z ${REXEC} ]; then
  echo "R not found, please ensure R is available and try again."
  exit 1

echo "install.packages(\"${pkg}\", repos=\"https://cran.rstudio.com\")" | R --no-save

Hope it’s useful to someone else, and if you do use it for something, let me know, I’d love to hear about it!