diff --git a/DESCRIPTION b/DESCRIPTION
index 1e2181f0e..ad2322573 100644
--- a/DESCRIPTION
+++ b/DESCRIPTION
@@ -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 = "kevin@rstudio.com"),
person("RStudio", role = c("cph"))
diff --git a/NEWS.md b/NEWS.md
index da181310c..6ad34fa60 100644
--- a/NEWS.md
+++ b/NEWS.md
@@ -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)
diff --git a/R/snapshot.R b/R/snapshot.R
index 84b889ec2..64734d8cf 100644
--- a/R/snapshot.R
+++ b/R/snapshot.R
@@ -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.
#'
@@ -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()
@@ -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)
@@ -907,3 +915,26 @@ renv_snapshot_fixup_renv <- function(records) {
# nocov end
}
+
+renv_snapshot_reprex <- function(lockfile) {
+
+ fmt <- "Lockfile generated by renv %s."
+ version <- sprintf(fmt, renv_package_version("renv"))
+
+ text <- c(
+ "",
+ "Lockfile
",
+ "```",
+ renv_lockfile_write(lockfile, file = NULL),
+ "```",
+ version,
+ " "
+ )
+
+ output <- paste(text, collapse = "\n")
+ class(output) <- "knit_asis"
+ attr(output, "knit_cacheable") <- NA
+
+ output
+
+}
diff --git a/man/snapshot.Rd b/man/snapshot.Rd
index df7f9f93e..15f1fa5c8 100644
--- a/man/snapshot.Rd
+++ b/man/snapshot.Rd
@@ -12,7 +12,8 @@ snapshot(
type = settings$snapshot.type(project = project),
packages = NULL,
prompt = interactive(),
- force = FALSE
+ force = FALSE,
+ reprex = FALSE
)
}
\arguments{
@@ -46,6 +47,9 @@ compatibility, \code{confirm} is accepted as an alias for \code{prompt}.}
\item{force}{Boolean; force generation of a lockfile even when pre-flight
validation checks have failed?}
+
+\item{reprex}{Boolean; generate output appropriate for embedding the lockfile
+as part of a \href{https://www.tidyverse.org/help/#reprex}{reprex}?}
}
\value{
The generated lockfile, as an \R object (invisibly). Note that