Recover clean, runnable code from a reprex captured in the wild. The code is returned invisibly, put on the clipboard, and possibly written to file. Three different functions address various forms of wild-caught reprex.

reprex_invert(input = NULL, outfile = NULL, venue = c("gh", "so", "ds",
  "r"), comment = opt("#>"))

reprex_clean(input = NULL, outfile = NULL, comment = opt("#>"))

reprex_rescue(input = NULL, outfile = NULL, prompt = getOption("prompt"),
  continue = getOption("continue"))

Arguments

input

Character. If has length one and lacks a terminating newline, interpreted as the path to a file containing reprex code. Otherwise, assumed to hold reprex code as character vector. If not provided, the clipboard is consulted for input.

outfile

Optional basename for output file. When NULL, no file is left behind. If outfile = "foo", expect an output file in current working directory named foo_clean.R. If outfile = NA, expect on output file in a location and with basename derived from input, if a path, or in current working directory with basename derived from tempfile() otherwise.

venue

Character. Must be one of the following:

  • "gh" for GitHub, the default

  • "so" for Stack Overflow

  • "ds" for Discourse, e.g., community.rstudio.com. Note: this is currently just an alias for "gh"!

  • "r" or "R" for a runnable R script, with commented output interleaved

comment

regular expression that matches commented output lines

prompt

character, the prompt at the start of R commands

continue

character, the prompt for continuation lines

Value

Character vector holding just the clean R code, invisibly

Functions

  • reprex_invert: Attempts to reverse the effect of reprex(). When venue = "r", this just becomes a wrapper around reprex_clean().

  • reprex_clean: Assumes R code is top-level, possibly interleaved with commented output, e.g., a displayed reprex copied from GitHub or the direct output of reprex(..., venue = "R"). This function removes commented output.

  • reprex_rescue: Assumes R code lines start with a prompt and that printed output is top-level, e.g., what you'd get from copy/paste from the R Console. Removes lines of output and strips prompts from lines holding R commands.

Examples

# NOT RUN {
## a rendered reprex can be inverted, at least approximately
x <- reprex({
  #' Some text
  #+ chunk-label-and-options-cannot-be-recovered, message = TRUE
  (x <- 1:4)
  #' More text
  y <- 2:5
  x + y
}, show = FALSE, advertise = FALSE)
writeLines(x)
x <- reprex_invert(x)
writeLines(x)
# }# NOT RUN {
## a displayed reprex can be cleaned of commented output
x <- c(
  "## a regular comment, which is retained",
  "(x <- 1:4)",
  "#> [1] 1 2 3 4",
  "median(x)",
  "#> [1] 2.5"
  )
out <- reprex_clean(x)
writeLines(out)

## round trip with reprex(..., venue = "R")
code_in <- c("x <- rnorm(2)", "min(x)")
res <- reprex(input = code_in, venue = "R", advertise = FALSE)
res
(code_out <- reprex_clean(res))
identical(code_in, code_out)
# }# NOT RUN {
## rescue a reprex that was copied from a live R session
x <- c(
  "> ## a regular comment, which is retained",
  "> (x <- 1:4)",
  "[1] 1 2 3 4",
  "> median(x)",
  "[1] 2.5"
)
out <- reprex_rescue(x)
writeLines(out)
# }