Skip to content

Instantly share code, notes, and snippets.

@jimhester
Last active Jul 11, 2018
Embed
What would you like to do?
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