Approaches to specify package versions/environment within a script?

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)")


assert_library(c("glue" = ">=1.3.0", "dplyr" = "github::tidyverse/dplyr@release")


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:

  1. Put your script inside a package, and use DESCRIPTION to specify versions
  2. Use packrat to store the source code for the packages you use
  3. 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.


There is a new :package: in development to help with setup an R environment with specific version of package.

You could be interested in following the evolution.

1 Like

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.

Thanks, maybe I will get ambitious and give it a try.

If you want to build a wrapper, you can also use sessioninfo :package:

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 :package: 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 :package: if the assertion is false.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.