Within the context of a package, there are tools to make sure that you are using a particular version of package imports, but are there tools to be more specific about this in a script? E.g.:
library("glue (>=1.3.0)")
or
assert_library(c("glue" = ">=1.3.0", "dplyr" = "github::tidyverse/dplyr@release")
Etc.
Within packages and installation tools, there are ways to be specific about versions. But in many contexts in R, the relevant unit is a script or code chunk alone, so it seems like it could be helpful to have the option of specificity within the script itself (as opposed to within a separate DESCRIPTION or packrat lockfile). Examples would be reprexes, background jobs in RStudio 1.2, or make builds.
In discussions around reproducible research, there are different 'layers of reproducibility' approaches:
Put your script inside a package, and use DESCRIPTION to specify versions
Use packrat to store the source code for the packages you use
Use docker to also store system dependencies
But these all use external files, and script itself still just says library(pkg). Do approaches exist to be specific about package versions within a script?
I'm not sure if there is a package/function that does this for you automatically, but it's easy to write one yourself since you have packageVersion function you can use to do what you want.
Yes, I'm very excited about that package. Like packrat, my understanding is that it will take the approach of using an external lockfile.
In the context of a single script/reprex/code chunk, I do feel there is an opportunity for in-line specification of the type of information that is contained in lockfiles and DESCRIPTION imports. I suppose that is what the "sessioninfo" part of reprex is for.
If you want to build a wrapper, you can also use sessioninfo
sessioninfo::package_info(c("dplyr", "ggplot2")
This will get you a tibble with some interesting colums like ondiskversion or source (CRAN, Github (tidyverse/dplyr@6c9a235)...)
Also utils::compareVersion can be useful.
pacman has also some version function with a specific pacman::p_install_version() that will install new version of a package if the one requested is not installed or if the version requested is above the one available currently in the system. Could be suitable to what you seek if you are not against the installation of the if the assertion is false.