{loadinstall} is a package for loading or installing a package in R.
It works very simply: if you already have the package installed, it will just load it; if not, it will both install and load the package.
No more needing to know which package is present or not in the environment you are working on!
Sharing your Rmarkdown or Quarto documents will be easier by using the dynamic_require() set of functions.
You can install the development version of {loadinstall} from GitHub with:
# install.packages("devtools")
devtools::install_github("flaviaerius/loadinstall")This is a basic example which shows you how to solve a common problem:
library(loadinstall)
## basic example code
dynamic_require("dplyr")
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
## github install
github_dynamic_require("ggpubr", "kassambara/ggpubr")
#> Loading required package: ggplot2
## bioconductor install
bioc_dynamic_require("edgeR")
#> Loading required package: limmaSince {loadinstall} per se is a package that needs to be installed, a workaround is to include the following block of code in the beggining of your R script or an initial chunk in Quarto or Rmarkdown to install or load the package:
if(!require("loadinstall")) {
if(!require("remotes", quietly = TRUE)) {
utils::install.packages("remotes", repos = "http://cran.us.r-project.org")
}
remotes::install_github("flaviaerius/loadinstall")
require("loadinstall")
}