Happiness is in the little things. Here are a couple of tricks for installing and loading R packages in one go that made me exclaim “flippin’ finally!” when I found out about them.
The first is a trick I learned from Gerald Belton:
if (!require(devtools)) install.packages('devtools'); library(devtools)
I still use this trick for packages stored on github, e.g.:
if (!require(buildmer)) install_github("cvoeten/buildmer"); library(buildmer)
For packages on CRAN I use
pacman::p_load(), which accomplishes the same goal with a more concise syntax. Example:
It can also handle multiple packages at once:
pacman::p_load(doBy, # like aggregate() on steroids glmmTMB # like lm+glm+lme4 on steroids )
Given that I often use the same packages, I use this at the beginning of all my R scripts, together with the following line of code to make sure that I am using the latest stable version of all CRAN packages:
update.packages(ask = FALSE, type="binary")
Did you enjoy this? Consider joining my on-line course “First steps in data analysis with R” and learn data analysis from zero to hero!