From a4e3efe80a0c164230cc96dfe97563c99b8e94d8 Mon Sep 17 00:00:00 2001 From: Jenny Bryan Date: Fri, 15 Jan 2021 14:02:11 -0800 Subject: [PATCH] Introduce reprex.current_venue option cc @kevinushey Relates to #313 --- NEWS.md | 2 ++ R/reprex-options.R | 12 +++++++----- R/reprex_render.R | 3 ++- man/reprex_options.Rd | 12 +++++++----- tests/testthat/test-reprex-options.R | 8 ++++++++ 5 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 tests/testthat/test-reprex-options.R diff --git a/NEWS.md b/NEWS.md index 7c513c51..588e0df3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -36,6 +36,8 @@ * Experimental venue "rtf" now works on Windows, to approximately the same extent as it works on macOS. In both cases, it still requires a working installation of the highlight command line tool (#331). +* `reprex.current_venue` is a new read-only option that is set during `reprex_render()`. Other packages can use it to generate `reprex()`-compatible, `venue`-specific output. + ## Dependency changes R 3.1 and R 3.2 are no longer explicitly supported or tested. Our general practice is to support the current release (4.0, at time of writing), devel, and the 4 previous versions of R (3.6, 3.5, 3.4, 3.3). diff --git a/R/reprex-options.R b/R/reprex-options.R index 63dd5835..df1afd2e 100644 --- a/R/reprex-options.R +++ b/R/reprex-options.R @@ -13,11 +13,13 @@ #' * `reprex.tidyverse_quiet` #' * `reprex.std_out_err` #' -#' A few more options exist, but are only consulted in specific situations: -#' * `reprex.venue`: Only consulted by [reprex_selection()]. [reprex()] -#' itself reveals the possible values for `venue` in the "Usage" section -#' of its help file and defaults to the first value, in the usual -#' [match.arg()] way. +#' A few more options exist, but are only relevant to specific situations: +#' * `reprex.venue`: Can be used to control the `venue` used by the +#' [reprex_selection()] addin. +#' * `reprex.current_venue`: Read-only option that is set during +#' [reprex_render()]. Other packages that want to generate reprex-compatible +#' output can consult it via `getOption("reprex.current_venue")`, if they want +#' to tailor their output to the `venue`. #' * `reprex.highlight.hl_style`: Only relevant to `venue = "rtf`. Details are #' in the article #' [reprex venue RTF](https://reprex.tidyverse.org/articles/articles/rtf.html). diff --git a/R/reprex_render.R b/R/reprex_render.R index d398871c..6058eee3 100644 --- a/R/reprex_render.R +++ b/R/reprex_render.R @@ -86,7 +86,8 @@ reprex_render_impl <- function(input, rlang_trace_top_env = globalenv(), `rlang:::force_unhandled_error` = TRUE, rlang_backtrace_on_error = "full", - crayon.enabled = FALSE + crayon.enabled = FALSE, + reprex.current_venue = venue ) if (new_session) { out <- tryCatch( diff --git a/man/reprex_options.Rd b/man/reprex_options.Rd index 5a9e490f..15145992 100644 --- a/man/reprex_options.Rd +++ b/man/reprex_options.Rd @@ -19,12 +19,14 @@ ones: \item \code{reprex.std_out_err} } -A few more options exist, but are only consulted in specific situations: +A few more options exist, but are only relevant to specific situations: \itemize{ -\item \code{reprex.venue}: Only consulted by \code{\link[=reprex_selection]{reprex_selection()}}. \code{\link[=reprex]{reprex()}} -itself reveals the possible values for \code{venue} in the "Usage" section -of its help file and defaults to the first value, in the usual -\code{\link[=match.arg]{match.arg()}} way. +\item \code{reprex.venue}: Can be used to control the \code{venue} used by the +\code{\link[=reprex_selection]{reprex_selection()}} addin. +\item \code{reprex.current_venue}: Read-only option that is set during +\code{\link[=reprex_render]{reprex_render()}}. Other packages that want to generate reprex-compatible +output can consult it via \code{getOption("reprex.current_venue")}, if they want +to tailor their output to the \code{venue}. \item \code{reprex.highlight.hl_style}: Only relevant to \verb{venue = "rtf}. Details are in the article \href{https://reprex.tidyverse.org/articles/articles/rtf.html}{reprex venue RTF}. diff --git a/tests/testthat/test-reprex-options.R b/tests/testthat/test-reprex-options.R new file mode 100644 index 00000000..a461ffc7 --- /dev/null +++ b/tests/testthat/test-reprex-options.R @@ -0,0 +1,8 @@ +test_that("reprex.current_venue is set", { + skip_on_cran() + input <- "getOption('reprex.current_venue')" + ret <- reprex(input = paste0(input, "\n")) + expect_match(ret, "gh", all = FALSE) + ret <- reprex(input = paste0(input, "\n"), venue = "html") + expect_match(ret, "html", all = FALSE) +})