reprex {reprex} | R Documentation |
Render a reprex
Description
Run a bit of R code using rmarkdown::render()
and write the rendered result
to user's clipboard. If the clipboard is unavailable, the file containing
the rendered result is opened for manual copy. The goal is to make it easy to
share a small reproducible example ("reprex"), e.g., in a GitHub issue.
Reprex source can be
read from clipboard
provided directly as expression, character vector, or string
read from file
read from current selection or active document in RStudio
reprex can also be used for syntax highlighting (with or without rendering); see below for more.
Usage
reprex(
x = NULL,
input = NULL,
wd = NULL,
venue = c("gh", "r", "rtf", "html", "slack", "so", "ds"),
render = TRUE,
advertise = NULL,
session_info = opt(FALSE),
style = opt(FALSE),
comment = opt("#>"),
tidyverse_quiet = opt(TRUE),
std_out_err = opt(FALSE),
html_preview = opt(TRUE),
outfile = deprecated(),
show = deprecated(),
si = deprecated()
)
Arguments
x |
An expression. If not given, When the clipboard is structurally unavailable, e.g., on RStudio Server or
RStudio Cloud, |
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. When |
wd |
An optional filepath that is consulted when The most common use of |
venue |
Character. Must be one of the following (case insensitive):
|
render |
Logical. Whether to call |
advertise |
Logical. Whether to include a footer that describes when and
how the reprex was created. If unspecified, the option |
session_info |
Logical. Whether to include
|
style |
Logical. Whether to set the knitr chunk option |
comment |
Character. Prefix with which to comment out output, defaults
to |
tidyverse_quiet |
Logical. Sets the options |
std_out_err |
Logical. Whether to append a section for output sent to
stdout and stderr by the reprex rendering process. This can be necessary to
reveal output if the reprex spawns child processes or |
html_preview |
Logical. Whether to show rendered output in a viewer
(RStudio or browser). Always |
outfile |
in favor of |
show |
in favor of |
si |
Value
Character vector of rendered reprex, invisibly.
Details
The usual "code + commented output" is returned invisibly, written to file,
and, whenever possible, put on the clipboard. An HTML preview displays in
RStudio's Viewer pane, if available, or in the default browser, otherwise.
Leading "> "
prompts, are stripped from the input code. Read more at
https://reprex.tidyverse.org/.
reprex sets specific knitr options:
Chunk options default to
collapse = TRUE
,comment = "#>"
,error = TRUE
. Note thaterror = TRUE
, because a common use case is bug reporting.reprex also sets knitr's
upload.fun
. It defaults toknitr::imgur_upload()
so figures produced by the reprex appear properly on GitHub, Stack Overflow, Discourse, and Slack. Note thatimgur_upload()
requires the packages httr and xml2. Whenvenue = "r"
,upload.fun
is set toidentity()
, so that figures remain local. In that case, you may also want to provide a filepath toinput
or setwd
, to control where the reprex files are written. You can supplement or override these options with special comments in your code (see examples).
Error backtraces
To use rlang::last_error()
or rlang::last_trace()
within a reprex,
you must place them in a different "chunk" to the code that generates an
error. The easiest way to do is to insert a line containing the special
comment #'
after error-causing code:
f <- function() rlang::abort('foo') f() #' rlang::last_error() rlang::last_trace()
Read more in rlang's documentation: Errors in RMarkdown.
Syntax highlighting
A secondary use case for reprex is to produce syntax highlighted code
snippets, with or without rendering, to paste into applications like
Microsoft Word, PowerPoint, or Keynote. Use venue = "rtf"
for this.
This feature is experimental and requires the installation of the
highlight
command line tool. The "rtf"
venue is documented in its own article
Examples
## Not run:
# put some code like this on the clipboard
# (y <- 1:4)
# mean(y)
reprex()
# provide code as an expression
reprex(rbinom(3, size = 10, prob = 0.5))
reprex({y <- 1:4; mean(y)})
reprex({y <- 1:4; mean(y)}, style = TRUE)
# note that you can include newlines in those brackets
# in fact, that is often a good idea
reprex({
x <- 1:4
y <- 2:5
x + y
})
## provide code via character vector
reprex(input = c("x <- 1:4", "y <- 2:5", "x + y"))
## if just one line, terminate with '\n'
reprex(input = "rnorm(3)\n")
## customize the output comment prefix
reprex(rbinom(3, size = 10, prob = 0.5), comment = "#;-)")
# override a default chunk option
reprex({
#+ setup, include = FALSE
knitr::opts_chunk$set(collapse = FALSE)
#+ actual-reprex-code
(y <- 1:4)
median(y)
})
# add prose, use general markdown formatting
reprex({
#' # A Big Heading
#'
#' Look at my cute example. I love the
#' [reprex](https://github.com/tidyverse/reprex#readme) package!
y <- 1:4
mean(y)
}, advertise = FALSE)
# read reprex from file and write resulting files to that location
tmp <- file.path(tempdir(), "foofy.R")
writeLines(c("x <- 1:4", "mean(x)"), tmp)
reprex(input = tmp)
list.files(dirname(tmp), pattern = "foofy")
# clean up
file.remove(list.files(dirname(tmp), pattern = "foofy", full.names = TRUE))
# write reprex to file AND keep figure local too, i.e. don't post to imgur
tmp <- file.path(tempdir(), "foofy")
dir.create(tmp)
reprex({
#+ setup, include = FALSE
knitr::opts_knit$set(upload.fun = identity)
#+ actual-reprex-code
#' Some prose
## regular comment
(x <- 1:4)
median(x)
plot(x)
}, wd = tmp)
list.files(dirname(tmp), pattern = "foofy")
# clean up
unlink(tmp, recursive = TRUE)
## target venue = R, also good for email or Slack snippets
ret <- reprex({
x <- 1:4
y <- 2:5
x + y
}, venue = "R")
ret
## target venue = html
ret <- reprex({
x <- 1:4
y <- 2:5
x + y
}, venue = "html")
ret
## include prompt and don't comment the output
## use this when you want to make your code hard to execute :)
reprex({
#+ setup, include = FALSE
knitr::opts_chunk$set(comment = NA, prompt = TRUE)
#+ actual-reprex-code
x <- 1:4
y <- 2:5
x + y
})
## leading prompts are stripped from source
reprex(input = c("> x <- 1:3", "> median(x)"))
## End(Not run)