Render bits of R code for sharing, e.g., on GitHub or StackOverflow.
reprex? It’s a reproducible example, as coined by Romain Francois.
Given R code on the clipboard, selected in RStudio, as an expression (quoted or not), or in a file …
render()arguments, knitr options, and Pandoc options.
Get resulting runnable code + output as
The result is returned invisibly, written to a file and, if possible, placed on the clipboard. Preview an HTML version in RStudio viewer or default browser.
Install from CRAN:
or get a development version from GitHub:
sudo apt-get installor
sudo yum installinstallation methods should work for both xclip and xsel.
Let’s say you copy this code onto your clipboard (or, on RStudio Server or Cloud, select it):
Then callreprex(), where the default target venue is GitHub:reprex()
A nicely rendered HTML preview will display in RStudio's Viewer (if you’re in RStudio) or your default browser otherwise.
The relevant bit of GitHub-flavored Markdown is ready to be pasted from your clipboard (on RStudio Server or Cloud, you will need to copy this yourself):``` r (y  1 2 3 4 mean(y) #>  2.5 ```
Here’s what that Markdown would look like rendered in a GitHub issue:(y  1 2 3 4 mean(y) #>  2.5
Anyone else can copy, paste, and run this immediately.
In addition to GitHub, this Markdown also works on Stack Overflow and Discourse. Those venues can be formally requested viavenue = "so"andvenue = "ds", but they are just aliases forvenue = "gh".
Instead of reading from the clipboard, you can:
reprex(mean(rnorm(10)))to get code from expression.
reprex(input = "mean(rnorm(10))\n")gets code from character vector (detected via length or terminating newline). Leading prompts are stripped from input source:
reprex(input = "> median(1:3)\n")produces same output as
reprex(input = "median(1:3)\n")
reprex(input = "my_reprex.R")gets code from file
Use one of the RStudio add-ins to use the selected text or current file.
But wait, there’s more!
Get slightly different Markdown, optimized for Slack messages, with
reprex(..., venue = "slack").
Get a runnable R script, augmented with commented output, with
reprex(..., venue = "R"). This is useful for Slack code snippets, email, etc.
Get html with
reprex(..., venue = "html"). Useful for sites that don't support Markdown.
Prepare (un)rendered, syntax-highlighted code snippets to paste into Keynote or PowerPoint, with
reprex(..., venue = "rtf"). This feature is still experimental; see the associated article for more.
By default, figures are uploaded to imgur.com and the resulting URL is dropped into an inline image tag.
If you really need to reprex in a specific directory, use the
wdargument. For example,
reprex(wd = ".")requests the current working directory.
Append session info via
reprex(..., session_info = TRUE).
Get clean, runnable code from wild-caught reprexes with
reprex_invert()= the opposite of
reprex_clean(), e.g. when you copy/paste from GitHub or Stack Overflow
reprex_rescue(), when you’re dealing with copy/paste from R Console
Please note that the reprex project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.