Prepare reprexes for posting to GitHub issues, StackOverflow, or Slack snippets. What is a 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 …

Get resulting runnable code + output as

  • Markdown, formatted for target venue, e.g. gh or so, or as
  • R code, augmented with commented output.

Result is returned invisibly, placed on the clipboard, and written to a file. Preview an HTML version in RStudio viewer or default browser.

Installation

Install from CRAN:

install.packages("reprex")

or get a development version from GitHub:

devtools::install_github("tidyverse/reprex")

Usage

Let’s say you copy this code onto your clipboard:

(y <- 1:4)
mean(y)

Then call reprex(), where the default target venue is GitHub:

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:

``` r
(y <- 1:4)
#> [1] 1 2 3 4
mean(y)
#> [1] 2.5
```

Here’s what that Markdown would look like rendered in a GitHub issue:

(y <- 1:4)
#> [1] 1 2 3 4
mean(y)
#> [1] 2.5

Anyone else can copy, paste, and run this immediately.

Instead of reading from the clipboard, you can:

But wait, there’s more!

  • Set the target venue to Stack Overflow with reprex(..., venue = "so").

  • Get a runnable R script, augmented with commented output, with reprex(..., venue = "R"). This is useful for Slack, email, etc.

  • By default, figures are uploaded to imgur.com and resulting URL is dropped into an inline image tag.

  • Use the outfile argument to control where results are left behind. Use outfile = NA to work in current working directory.

  • Get clean, runnable code from wild-caught reprexes with