MyNixOS website logo
Description

Prepare Reproducible Example Code via the Clipboard.

Convenience wrapper that uses the 'rmarkdown' package to render small snippets of code to target formats that include both code and output. The goal is to encourage the sharing of small, reproducible, and runnable examples on code-oriented websites, such as <https://stackoverflow.com> and <https://github.com>, or in email. The user's clipboard is the default source of input code and the default target for rendered output. 'reprex' also extracts clean, runnable R code from various common formats, such as copy/paste from an R session.

reprex

CRAN_Status_Badge R-CMD-check Codecov test coverage

Overview

Prepare reprexes for posting to GitHub issues, StackOverflow, in Slack messages or snippets, or even to paste into PowerPoint or Keynote slides. What is a reprex? It’s a reproducible example, as coined by Romain Francois in a tweet from 2014.

Given R code on the clipboard, selected in RStudio, as an expression (quoted or not), or in a file …

  • run it via rmarkdown::render(),
  • with deliberate choices re: render() arguments, knitr options, and Pandoc options.

Get resulting runnable code + output as

  • Markdown, suitable for GitHub or Stack Overflow or Slack, or as
  • R code, augmented with commented output, or as
  • Plain HTML or (experimental) Rich Text

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.

Installation

Install from CRAN:

install.packages("reprex")

or get a development version from GitHub:

# install.packages("pak")
pak::pak("tidyverse/reprex")

On Linux, you probably want to install xclip or xsel, so reprex can access the X11 clipboard. This is 'nice to have', but not mandatory. The usual sudo apt-get install or sudo yum install installation methods should work for both xclip and xsel.

Usage

Let’s say you copy this code onto your clipboard (or, on RStudio Server or Cloud, select it):

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

Then call reprex(), 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: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.

In addition to GitHub, this Markdown also works on Stack Overflow and Discourse. Those venues can be formally requested via venue = "so" and venue = "ds", but they are just aliases for venue = "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 wd argument. 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()
    • 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.
Metadata

Version

2.1.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows