Skip to content

Commit

Permalink
use 'snapshot(reprex = TRUE)' to embed lockfile in reprex
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinushey committed Jan 15, 2021
1 parent 7bd669b commit 25be944
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: renv
Type: Package
Title: Project Environments
Version: 0.12.5-7
Version: 0.12.5-9
Authors@R: c(
person("Kevin", "Ushey", role = c("aut", "cre"), email = "[email protected]"),
person("RStudio", role = c("cph"))
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@

# renv 0.13.0 (UNRELEASED)

* `renv::snapshot()` gains the `reprex` argument. Set this to `TRUE` if you'd
like to embed an `renv` lockfile as part of a reproducible example, as
generated by the [`reprex`](https://www.tidyverse.org/help/#reprex-pkg)
package.

* `renv::status()` now reports packages that are referenced in a project's
scripts, but are neither installed in the project library nor recorded in the
lockfile. (#588)
Expand Down
33 changes: 32 additions & 1 deletion R/snapshot.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
#' @param force Boolean; force generation of a lockfile even when pre-flight
#' validation checks have failed?
#'
#' @param reprex Boolean; generate output appropriate for embedding the lockfile
#' as part of a [reprex](https://www.tidyverse.org/help/#reprex)?
#'
#' @return The generated lockfile, as an \R object (invisibly). Note that
#' this function is normally called for its side effects.
#'
Expand All @@ -97,7 +100,8 @@ snapshot <- function(project = NULL,
type = settings$snapshot.type(project = project),
packages = NULL,
prompt = interactive(),
force = FALSE)
force = FALSE,
reprex = FALSE)
{
renv_consent_check()
renv_scope_error_handler()
Expand Down Expand Up @@ -127,6 +131,10 @@ snapshot <- function(project = NULL,
if (is.null(lockfile))
return(new)

# if running as part of 'reprex', then render output inline
if (reprex)
return(renv_snapshot_reprex(new))

# TODO: do we still want to snapshot if the user cancels
# the R-level snapshot?
on.exit(renv_python_snapshot(project), add = TRUE)
Expand Down Expand Up @@ -907,3 +915,26 @@ renv_snapshot_fixup_renv <- function(records) {
# nocov end

}

renv_snapshot_reprex <- function(lockfile) {

fmt <- "<sup>Lockfile generated by renv %s.</sup>"
version <- sprintf(fmt, renv_package_version("renv"))

text <- c(
"<details style=\"margin-bottom: 10px;\">",
"<summary>Lockfile</summary>",
"```",
renv_lockfile_write(lockfile, file = NULL),
"```",
version,
"</details>"
)

output <- paste(text, collapse = "\n")
class(output) <- "knit_asis"
attr(output, "knit_cacheable") <- NA

output

}
6 changes: 5 additions & 1 deletion man/snapshot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 25be944

Please sign in to comment.