Last active
July 11, 2018 11:18
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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