un-reprex {reprex}R Documentation

Un-render a reprex

Description

Recover clean, runnable code from a reprex captured in the wild. The code is printed, returned invisibly, and written to the clipboard, if possible. Pick the function that deals with your problem:

Usage

reprex_invert(input = NULL, venue = c("gh", "so"), comment = "^#>")

reprex_clean(input = NULL, comment = "^#>")

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

Arguments

input

character, holding a wild-caught reprex as a character vector (length greater than one), string (length one with terminating newline), or file path (length one with no terminating newline). If not provided, the clipboard is consulted for input.

venue

"gh" for GitHub (default), "so" for StackOverflow, "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

Value

character vector holding just the clean R code, invisibly

Functions

Examples

## 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)
writeLines(x)
reprex_invert(x)
## 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"
  )
reprex_clean(x)

## Not run: 
## round trip with reprex(..., venue = "R")
code_in <- c("x <- rnorm(2)", "min(x)")
res <- reprex(input = code_in, venue = "R")
res
(code_out <- reprex_clean(res))
identical(code_in, code_out)

## End(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"
)
reprex_rescue(x)

[Package reprex version 0.1.1 Index]