Skip to content

Instantly share code, notes, and snippets.

@jimhester
Last active July 11, 2018 11:18
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimhester/d7aeb95bbed02f2985a87c2a3ede19f5 to your computer and use it in GitHub Desktop.
Save jimhester/d7aeb95bbed02f2985a87c2a3ede19f5 to your computer and use it in GitHub Desktop.
Run R code with different versions of package dependencies, this will work in the same session as long as you don't run into issues unloading the namespaces
with_pkg_version <- function(pkg, version, code, ...) {
if (isNamespaceLoaded(pkg)) {
unloadNamespace(pkg)
}
dir <- tempfile()
dir.create(dir)
on.exit(unlink(dir))
withr::with_libpaths(dir, action = "prefix", {
on.exit(unloadNamespace(pkg))
devtools::install_version(pkg, version, ...)
force(code)
})
}
with_pkg_version("ggplot2", "0.9.3", {
library(ggplot2)
ggsave("0.9.3.png",
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point())
})
with_pkg_version("ggplot2", "2.2.0", {
library(ggplot2)
ggsave("2.2.0.png",
ggplot(mpg, aes(displ, hwy, colour = class)) +
geom_point())
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment