• Stars
    star
    735
  • Rank 61,652 (Top 2 %)
  • Language
    R
  • License
    Other
  • Created about 9 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,496
star
2

dplyr

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

tidyverse

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

rvest

Simple web scraping for R
R
1,488
star
5

tidyr

Tidy Messy Data
R
1,369
star
6

purrr

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

readr

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

magrittr

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

datascience-box

Data Science Course in a Box
JavaScript
937
star
10

lubridate

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

readxl

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

glue

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

dtplyr

Data table backend for dplyr
R
661
star
14

tibble

A modern re-imagining of the data frame
R
659
star
15

multidplyr

A dplyr backend that partitions a data frame over multiple processes
R
640
star
16

vroom

Fast reading of delimited files
C++
618
star
17

stringr

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

forcats

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

dbplyr

Database (DBI) backend for dplyr
R
473
star
20

haven

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

modelr

Helper functions for modelling
R
401
star
22

googlesheets4

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

googledrive

Google Drive R API
R
321
star
24

style

The tidyverse style guide for R code
HTML
291
star
25

duckplyr

A drop-in replacement for dplyr, powered by DuckDB for performance.
R
236
star
26

design

Tidyverse design principles
R
217
star
27

tidyverse.org

Source of tidyverse.org
HTML
191
star
28

hms

A simple class for storing time-of-day values
R
137
star
29

nycflights13

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

tidyversedashboard

Tidyverse activity dashboard
R
71
star
31

tidy-dev-day

Tidyverse developer day
R
69
star
32

tidyeval

A guide to tidy evaluation
CSS
55
star
33

dsbox

Companion R package to Data Science Course in a Box
R
49
star
34

tidytemplate

A pkgdown template for core tidyverse packages
SCSS
45
star
35

blob

A simple S3 class for representing BLOBs
R
44
star
36

funs

Collection of low-level functions for working with vctrs
R
34
star
37

code-review

33
star
38

website-analytics

Web analytics for tidyverse + r-lib sites
R
28
star
39

tidyups

21
star
40

ggplot2-docs

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