Defensively install packages in R
This article is originally published at https://DrDoane.com
Often, your R code will rely on having one or more R packages available. A little defensive coding will save users of your code—including future-you—from having to figure out which packages you’re using and then having to manually install them. This lowers the extraneous cognitive load associated with running older or unfamiliar code.
if (!"tidyverse" %in% installed.packages()) install.package("tidyverse", deps = TRUE)
Or, if you prefer to always use blocks with IF statements:
if (!"tidyverse" %in% installed.packages()) { install.package("tidyverse", deps = TRUE) }
With a little persistence, you can extend this to dealing with multiple packages:
pkgs <- c("tidyverse", "openxlsx") install.packages(pkgs[!pkgs %in% installed.packages()], deps = TRUE)
Thanks for visiting r-craft.org
This article is originally published at https://DrDoane.com
Please visit source website for post related comments.