Here is what it could look like, using renv to create virtual environment for Rmd document rendering.
- Create a lockfile associated with a Rmd file
- Render the Rmd using the lockfile, powering
renv for a virtual environment for the rendering process.
# reproducible example
# create a dummy project folder
dir.create(proj_dir <- tempfile("dummy-reprex-proj"))
old <- setwd(proj_dir)
# with a dummy Rmd file
Rmd_file <- "test.Rmd"
xfun::write_utf8(c(
"---",
" title: test",
"---",
"",
"```{r}",
"library(glue)",
"```"
), Rmd_file)
# Creating the lockfile
# using a dummy project dir
dir.create(temp_dir <- tempfile("temp-renv-proj"))
# copy the Rmd file
file.copy(Rmd_file, temp_dir)
#> [1] TRUE
# generate a lockfile associated with this Rmd file
lockfile <- file.path(temp_dir, paste0("renv-", xfun::with_ext(basename(Rmd_file), "lock")))
renv::snapshot(project = temp_dir, lockfile = lockfile, confirm = FALSE)
#> The following package(s) will be updated in the lockfile:
#>
#> # CRAN ===============================
#> - Rcpp [* -> 1.0.3]
#> - base64enc [* -> 0.1-3]
#> - digest [* -> 0.6.23]
#> - evaluate [* -> 0.14]
#> - glue [* -> 1.3.1]
#> - highr [* -> 0.8]
#> - htmltools [* -> 0.4.0]
#> - jsonlite [* -> 1.6]
#> - knitr [* -> 1.26]
#> - magrittr [* -> 1.5]
#> - markdown [* -> 1.1]
#> - mime [* -> 0.8]
#> - rlang [* -> 0.4.2]
#> - rmarkdown [* -> 2.0]
#> - stringi [* -> 1.4.3]
#> - stringr [* -> 1.4.0]
#> - tinytex [* -> 0.18]
#> - xfun [* -> 0.11]
#> - yaml [* -> 2.2.0]
#>
#> # GitHub =============================
#> - renv [* -> rstudio/renv]
#>
#> * Lockfile written to 'C:/Users/DERVIE~1/AppData/Local/Temp/RtmpuIH8qz/temp-renv-proj3d54626a4e8a/renv-test.lock'.
# get back the lockfile
file.copy(lockfile, proj_dir)
#> [1] TRUE
list.files(proj_dir)
#> [1] "renv-test.lock" "test.Rmd"
# remove temp renv dir
unlink(temp_dir, recursive = TRUE)
# rendering the Rmd file using the lockfile
dir.create(temp_dir <- tempfile("temp-renv-proj"))
# copy the rmd file and the lockfile
to_copy <- c(rmd = "test.Rmd", lockfile = "renv-test.lock")
file.copy(to_copy, temp_dir)
#> [1] TRUE TRUE
# restore Rmd environment and render
html_file <- withr::with_dir(temp_dir, {
callr::r(function() {
renv::init(bare = TRUE, restart = FALSE)
renv::deactivate()
renv::restore(lockfile = "renv-test.lock")
rmarkdown::render("test.Rmd")
})
})
# get the rendered file
file.copy(html_file, proj_dir)
#> [1] TRUE
# remove temp renv dir
unlink(temp_dir, recursive = TRUE)
# The files has been rendered using its associated lockfile
list.files(proj_dir)
#> [1] "renv-test.lock" "test.html" "test.Rmd"
# remove reprex stuff
setwd(old)
unlink(proj_dir, recursive = TRUE)
Created on 2020-01-06 by the reprex package (v0.3.0)
This is one way of doing it. I think there is other (using renv::dependencies and renv::hydrate for example)
Also, I think {capsule} could be a good place to host this kind of feature. I'll look into how it could fit and see what the maintener think about this.
I think this kind of functions could be useful to have in a package. What do you think ? I am think of doing a prototype
to think about how best do that.
Is this something that could be useful ? What do you think of this example ?
I am curious of your thoughts on this. Thank you !