• Stars
    star
    719
  • Rank 59,280 (Top 2 %)
  • Language
    R
  • License
    Other
  • Created over 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render bits of R code for sharing, e.g., on GitHub or StackOverflow.

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.

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

More Repositories

1

ggplot2

An implementation of the Grammar of Graphics in R
R
6,212
star
2

dplyr

dplyr: A grammar of data manipulation
R
4,583
star
3

tidyverse

Easily install and load packages from the tidyverse
R
1,542
star
4

rvest

Simple web scraping for R
R
1,428
star
5

tidyr

Tidy Messy Data
R
1,298
star
6

purrr

A functional programming toolkit for R
R
1,202
star
7

readr

Read flat files (csv, tsv, fwf) into R
R
971
star
8

magrittr

Improve the readability of R code with the pipe
R
946
star
9

datascience-box

Data Science Course in a Box
JavaScript
878
star
10

lubridate

Make working with dates in R just that little bit easier
R
705
star
11

readxl

Read excel files (.xls and .xlsx) into R ๐Ÿ–‡
C++
699
star
12

glue

Glue strings to data in R. Small, fast, dependency free interpreted string literals.
R
670
star
13

dtplyr

Data table backend for dplyr
R
649
star
14

multidplyr

A dplyr backend that partitions a data frame over multiple processes
R
632
star
15

tibble

A modern re-imagining of the data frame
R
629
star
16

vroom

Fast reading of delimited files
C++
597
star
17

stringr

A fresh approach to string manipulation in R
R
555
star
18

forcats

๐Ÿˆ๐Ÿˆ๐Ÿˆ๐Ÿˆ: tools for working with categorical variables (factors)
R
528
star
19

dbplyr

Database (DBI) backend for dplyr
R
446
star
20

haven

Read SPSS, Stata and SAS files from R
C
415
star
21

modelr

Helper functions for modelling
R
398
star
22

googlesheets4

Google Spreadsheets R API (reboot of the googlesheets package)
R
341
star
23

googledrive

Google Drive R API
R
308
star
24

style

The tidyverse style guide for R code
HTML
281
star
25

design

Tidyverse design principles
R
203
star
26

tidyverse.org

Source of tidyverse.org
HTML
182
star
27

hms

A simple class for storing time-of-day values
R
136
star
28

nycflights13

An R data package containing all out-bound flights from NYC in 2013 + useful metdata
R
120
star
29

tidyversedashboard

Tidyverse activity dashboard
R
70
star
30

tidy-dev-day

Tidyverse developer day
57
star
31

tidyeval

A guide to tidy evaluation
CSS
53
star
32

dsbox

Companion R package to Data Science Course in a Box
R
46
star
33

blob

A simple S3 class for representing BLOBs
R
42
star
34

tidytemplate

A pkgdown template for core tidyverse packages
SCSS
42
star
35

funs

Collection of low-level functions for working with vctrs
R
30
star
36

website-analytics

Web analytics for tidyverse + r-lib sites
R
27
star
37

code-review

24
star
38

tidyups

19
star
39

ggplot2-docs

ggplot2 documentation. Auto-generated from ggplot2 sources by pkgdown
HTML
9
star