Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option for appending lockfile to reprex #314

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
^LICENSE\.md$
^\.github/workflows/R-CMD-check\.yaml$
^\.github/workflows/pkgdown\.yaml$
^WORDLIST$
^R/tags$
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Suggests:
covr,
fortunes,
miniUI,
renv,
rprojroot,
rstudioapi,
sessioninfo,
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
`knit: reprex::reprex_render` in the YAML, which causes the RStudio "Knit"
button to use `reprex_render()`.

* `reprex()` gains the `renv_lockfile` argument. When `TRUE`, a lockfile generated by the [renv](https://rstudio.github.io/renv/) package will be appended to the `reprex`. (#313, @kevinushey)

* The `si` argument of `reprex()` is now `session_info`. Being explicit seems more important than saving characters, given auto-completions.

* The `show` argument of `reprex()` is now `html_preview`, for the sake of consistency with other R Markdown output formats.
Expand Down
2 changes: 2 additions & 0 deletions R/prex.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ prex <- function(x = NULL,

advertise = FALSE, # <-- different from reprex
session_info = opt(FALSE),
renv_lockfile = opt(FALSE),
style = opt(FALSE),
html_preview = opt(TRUE),
comment = opt("#>"),
Expand All @@ -58,6 +59,7 @@ prex <- function(x = NULL,

advertise = advertise,
session_info = session_info,
renv_lockfile = renv_lockfile,
style = style,
comment = comment,
tidyverse_quiet = tidyverse_quiet,
Expand Down
10 changes: 9 additions & 1 deletion R/reprex-addin.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ reprex_addin <- function() { # nocov start
"Append session info",
getOption("reprex.session_info", FALSE)
),
shiny::checkboxInput(
"renv_lockfile",
"Append lockfile",
getOption("reprex.renv_lockfile", FALSE)
),
shiny::checkboxInput(
"html_preview",
"Preview HTML",
Expand All @@ -91,6 +96,7 @@ reprex_addin <- function() { # nocov start
input$venue,
input$source_file,
as.logical(input$session_info),
as.logical(input$renv_lockfile),
as.logical(input$html_preview)
))
})
Expand All @@ -101,7 +107,8 @@ reprex_addin <- function() { # nocov start
}

reprex_guess <- function(source, venue = "gh", source_file = NULL,
session_info = FALSE, html_preview = FALSE) {
session_info = FALSE, renv_lockfile = FALSE,
html_preview = FALSE) {
reprex_input <- switch(
source,
clipboard = NULL,
Expand All @@ -114,6 +121,7 @@ reprex_guess <- function(source, venue = "gh", source_file = NULL,
input = reprex_input,
venue = venue,
session_info = session_info,
renv_lockfile = renv_lockfile,
html_preview = html_preview
)
}
Expand Down
2 changes: 2 additions & 0 deletions R/reprex-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#' ones:
#' * `reprex.advertise`
#' * `reprex.session_info` (previously, `reprex.si`)
#' * `reprex.renv_lockfile`
#' * `reprex.style`
#' * `reprex.html_preview` (previously, `reprex.show`)
#' * `reprex.comment`
Expand All @@ -32,6 +33,7 @@
#' options(
#' reprex.advertise = FALSE,
#' reprex.session_info = TRUE,
#' reprex.renv_lockfile = TRUE,
#' reprex.style = TRUE,
#' reprex.html_preview = FALSE,
#' reprex.comment = "#;-)",
Expand Down
4 changes: 4 additions & 0 deletions R/reprex.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
#' [sessioninfo::session_info()], if available, or [sessionInfo()] at the end
#' of the reprex. When `venue` is "gh", the session info is wrapped in a
#' collapsible details tag. Read more about [opt()].
#' @param renv_lockfile Logical. Whether to include a lockfile generated
#' by [renv::snapshot()]. Read more about [opt()].
#' @param style Logical. Whether to set the knitr chunk option `tidy =
#' "styler"`, which re-styles code with the [styler
#' package](https://styler.r-lib.org). Read more about [opt()].
Expand Down Expand Up @@ -248,6 +250,7 @@ reprex <- function(x = NULL,

advertise = NULL,
session_info = opt(FALSE),
renv_lockfile = opt(FALSE),
style = opt(FALSE),
comment = opt("#>"),
tidyverse_quiet = opt(TRUE),
Expand Down Expand Up @@ -280,6 +283,7 @@ reprex <- function(x = NULL,

advertise = advertise,
session_info = session_info,
renv_lockfile = renv_lockfile,
style = style,
html_preview = html_preview,
comment = comment,
Expand Down
56 changes: 55 additions & 1 deletion R/reprex_document.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ reprex_document <- function(venue = c("gh", "r", "rtf", "html", "so", "ds"),

advertise = NULL,
session_info = opt(FALSE),
renv_lockfile = opt(FALSE),
style = opt(FALSE),
comment = opt("#>"),
tidyverse_quiet = opt(TRUE),
Expand All @@ -52,14 +53,19 @@ reprex_document <- function(venue = c("gh", "r", "rtf", "html", "so", "ds"),

advertise <- set_advertise(advertise, venue)
session_info <- arg_option(session_info)
renv_lockfile <- arg_option(renv_lockfile)
style <- arg_option(style)
style <- style_requires_styler(style)
# html_preview is actually an input for for reprex_render()
comment <- arg_option(comment)
tidyverse_quiet <- arg_option(tidyverse_quiet)
std_out_err <- arg_option(std_out_err)

stopifnot(is_toggle(advertise), is_toggle(session_info), is_toggle(style))
stopifnot(
is_toggle(advertise), is_toggle(session_info),
is_toggle(renv_lockfile), is_toggle(style)
)

stopifnot(is.character(comment))
stopifnot(is_toggle(tidyverse_quiet), is_toggle(std_out_err))

Expand Down Expand Up @@ -115,6 +121,12 @@ reprex_document <- function(venue = c("gh", "r", "rtf", "html", "so", "ds"),
)
}

if (isTRUE(renv_lockfile)) {
input_lines <- c(
input_lines, "", lf(venue, details = venue %in% c("gh", "html"))
)
}

write_lines(input_lines, knit_input)
}
}
Expand Down Expand Up @@ -187,3 +199,45 @@ session_info_string <- function() {
"sessionInfo()"
}
}

lf <- function(venue, details = FALSE) {
txt <- lf_string(venue)
if (details) {
txt <- c(
"<details style=\"margin-bottom:10px;\">",
"<summary>Lockfile</summary>",
txt,
"</details>"
)
}
txt
}

lf_string <- function(venue) {
if (!rlang::is_installed("renv")) {
warning("renv is not installed; cannot generate lockfile")
character()
} else {

chunk <- c(
"```{r renv-lockfile, echo=FALSE, comment=''}",
"options(renv.verbose = FALSE)",
"renv::snapshot(type = 'packrat', confirm = FALSE)",
"writeLines(readLines('renv.lock'))",
"```"
)

ad <- c(
"Lockfile generated by the renv package ",
"(v`r utils::packageVersion('renv')`)"
)

if (venue %in% c("gh", "so", "html")) {
ad <- c("<sup>", ad, "</sup>")
}

c(chunk, ad)

}

}
15 changes: 11 additions & 4 deletions R/reprex_impl.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ reprex_impl <- function(x_expr = NULL,

advertise = NULL,
session_info = opt(FALSE),
renv_lockfile = opt(FALSE),
style = opt(FALSE),
comment = opt("#>"),
tidyverse_quiet = opt(TRUE),
Expand All @@ -19,6 +20,7 @@ reprex_impl <- function(x_expr = NULL,

advertise <- set_advertise(advertise, venue)
session_info <- arg_option(session_info)
renv_lockfile <- arg_option(renv_lockfile)
style <- arg_option(style)
style <- style_requires_styler(style)
html_preview <- arg_option(html_preview)
Expand All @@ -29,8 +31,12 @@ reprex_impl <- function(x_expr = NULL,

if (!is.null(input)) stopifnot(is.character(input))
if (!is.null(outfile)) stopifnot(is.character(outfile) || is.na(outfile))
stopifnot(is_toggle(advertise), is_toggle(session_info), is_toggle(style))
stopifnot(is_toggle(html_preview), is_toggle(render))

stopifnot(
is_toggle(advertise), is_toggle(session_info), is_toggle(renv_lockfile),
is_toggle(style), is_toggle(html_preview), is_toggle(render)
)

stopifnot(is.character(comment))
stopifnot(is_toggle(tidyverse_quiet), is_toggle(std_out_err))

Expand Down Expand Up @@ -58,7 +64,7 @@ reprex_impl <- function(x_expr = NULL,

reprex_document_options <- list(
venue = venue,
advertise = advertise, session_info = session_info,
advertise = advertise, session_info = session_info, renv_lockfile = renv_lockfile,
style = style, html_preview = html_preview, comment = comment,
tidyverse_quiet = tidyverse_quiet, std_out_err = std_out_err
)
Expand Down Expand Up @@ -89,7 +95,7 @@ reprex_impl <- function(x_expr = NULL,
if (clipboard_available()) {
clipr::write_clip(out_lines)
message("Rendered reprex is on the clipboard.")
} else if (is_interactive()) {
} else if (rlang::is_interactive()) {
clipr::dr_clipr()
message(
"Unable to put result on the clipboard. How to get it:\n",
Expand Down Expand Up @@ -150,6 +156,7 @@ remove_defaults <- function(x) {
venue = "gh",
advertise = TRUE,
session_info = FALSE,
renv_lockfile = FALSE,
style = FALSE,
html_preview = TRUE,
comment = "#>",
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ knitr
knitr's
knitrthemesoverview
lifecycle
lockfile
macOS
McBain
metapackage
Expand All @@ -51,6 +52,7 @@ php
rclickhandbuch
RCurl
realise
renv
regexes
repr
reprex's
Expand Down
4 changes: 4 additions & 0 deletions man/reprex.Rd

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

4 changes: 4 additions & 0 deletions man/reprex_document.Rd

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

2 changes: 2 additions & 0 deletions man/reprex_options.Rd

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

2 changes: 1 addition & 1 deletion tests/spelling.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
if(requireNamespace('spelling', quietly = TRUE))
if (requireNamespace('spelling', quietly = TRUE))
spelling::spell_check_test(vignettes = TRUE, error = FALSE,
skip_on_cran = TRUE)
9 changes: 9 additions & 0 deletions tests/testthat/test-optionally.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ test_that("`session_info` can be set via option", {
expect_match(out, "session_*[iI]nfo", all = FALSE)
})

test_that("`renv_lockfile` can be set via option", {
skip_on_cran()
withr::with_options(
list(reprex.renv_lockfile = TRUE),
out <- reprex(1, render = FALSE)
)
expect_match(out, "renv", all = FALSE)
})

test_that("`advertise` can be set via option", {
skip_on_cran()
withr::with_options(
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-renv-lockfile.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("session info is omitted / included", {
skip_on_cran()
skip_if_not_installed("renv")

regex <- "renv"
input <- c("(y <- 1:4)", "mean(y)")
ret <- reprex(input = input)
expect_false(any(grepl(regex, ret)))
ret <- reprex(input = input, renv_lockfile = TRUE)
expect_match(ret, regex, all = FALSE)
})