-
Notifications
You must be signed in to change notification settings - Fork 78
/
reprex.html
307 lines (273 loc) · 14 KB
/
reprex.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Render a reprex — reprex • reprex</title>
<!-- jquery -->
<script src="https://code.jquery.com/jquery-3.1.0.min.js" integrity="sha384-nrOSfDHtoPMzJHjVTdCopGqIqeYETSXhZDFyniQ8ZHcVy08QesyHcnOUpMpqnmWq" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<!-- Font Awesome icons -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous">
<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../jquery.sticky-kit.min.js"></script>
<script src="../pkgdown.js"></script>
<!-- mathjax -->
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container template-reference-topic">
<header>
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="../index.html">reprex</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="../reference/index.html">Reference</a>
</li>
<li>
<a href="../news/index.html">News</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/jennybc/reprex">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
</header>
<div class="row">
<div class="col-md-9 contents">
<div class="page-header">
<h1>Render a reprex</h1>
</div>
<p>Run a bit of R code using
<code><a href='http://www.rdocumentation.org/packages/rmarkdown/topics/render'>rmarkdown::render</a>()</code>. The goal is to make it
easy to share a small reproducible example ("reprex"), e.g., in a GitHub
issue. Reprex source can be
<ul>
<li> read from clipboard
</li>
<li> read from current selection or active document
(<a href='reprex_addin.html'>"Render reprex" RStudio addin</a>)
</li>
<li> provided directly as expression, character vector, or string
</li>
<li> read from file
</li>
</ul></p>
<p>The usual "code + commented output" is returned invisibly, put on the
clipboard, and written to file. An HTML preview displays in RStudio's Viewer
pane, if available, or in the default browser, otherwise. Leading <code>"> "</code>
prompts, are stripped from the input code. Read more at
<a href = 'http://jennybc.github.io/reprex/'>http://jennybc.github.io/reprex/</a>.</p>
<pre><span class='fu'>reprex</span>(<span class='kw'>x</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>venue</span> <span class='kw'>=</span> <span class='fu'>c</span>(<span class='st'>"gh"</span>, <span class='st'>"so"</span>, <span class='st'>"r"</span>, <span class='st'>"R"</span>), <span class='kw'>si</span> <span class='kw'>=</span> <span class='fl'>FALSE</span>,
<span class='kw'>show</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>input</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>outfile</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>comment</span> <span class='kw'>=</span> <span class='st'>"#>"</span>,
<span class='kw'>opts_chunk</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>opts_knit</span> <span class='kw'>=</span> <span class='kw'>NULL</span>)</pre>
<h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a> Arguments</h2>
<dl class="dl-horizontal">
<dt>x</dt>
<dd>An expression. If not given, <code>reprex()</code> looks for code in
<code>input</code> or on the clipboard, in that order.</dd>
<dt>venue</dt>
<dd>"gh" for GitHub (default), "so" for StackOverflow, "r" or "R"
for a runnable R script, with commented output interleaved.</dd>
<dt>si</dt>
<dd>Whether to include the results of
<code><a href='http://www.rdocumentation.org/packages/devtools/topics/session_info'>devtools::session_info</a>()</code>, if available, or
<code>sessionInfo()</code> at the end of the reprex. When <code>venue =
"gh"</code> (the default), session info is wrapped in a collapsible details tag.</dd>
<dt>show</dt>
<dd>Whether to show rendered output in a viewer (RStudio or browser).
Defaults to <code>TRUE</code>.</dd>
<dt>input</dt>
<dd>Character. If has length one and lacks a terminating newline,
interpreted as the path to a file containing reprex code. Otherwise,
assumed to hold reprex code as character vector (length greater than one)
or string (with embedded newlines).</dd>
<dt>outfile</dt>
<dd>Optional basename for output files. When <code>NULL</code>
(default), reprex writes to temp files below the session temp directory. If
<code>outfile = "foo"</code>, expect output files in current working directory,
like <code>foo_reprex.R</code>, <code>foo_reprex.md</code>, and, if <code>venue = "R"</code>,
<code>foo_rendered.R</code>. If <code>outfile = NA</code>, expect output files in
current working directory with basename derived from the path in
<code>input</code>, if sensible, otherwise from <code>tempfile()</code>.</dd>
<dt>comment</dt>
<dd>Character. Prefix with which to comment out output, defaults
to <code>"#>"</code>.</dd>
<dt>opts_chunk, opts_knit</dt>
<dd>Named list. Optional
<a href = 'knitr chunk and package options'>http://yihui.name/knitr/options/</a>,
respectively, to supplement or override reprex defaults. See Details.</dd>
</dl>
<h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>
<p>Character vector of rendered reprex, invisibly.</p>
<h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>
<p>reprex sets specific <a href = 'knitr options'>http://yihui.name/knitr/options/</a>,
which you can supplement or override via the <code>opts_chunk</code> and
<code>opts_knit</code> arguments or via explicit calls to knitr in your reprex code
(see examples). If all you want to override is the <code>comment</code> option, use
the dedicated argument, e.g.<code>commment = "#;-)"</code>.</p>
<p><ul>
<li> Chunk options default to <code>collapse = TRUE</code>, <code>comment = "#>"</code>,
<code>error = TRUE</code>. These are options you normally set via
<code>knitr::opts_chunk$set()</code>. Note that <code>error = TRUE</code>, because a
common use case is bug reporting.
</li>
<li> Package options default to <code>upload.fun = knitr::imgur_upload</code>.
These are options you normally set via <code>knitr::opts_knit$set()</code>. The
<code>upload.fun</code> defaults to <code><a href='http://www.rdocumentation.org/packages/knitr/topics/imgur_upload'>imgur_upload</a></code> so figures
produced by the reprex appear properly on GitHub.
</li>
</ul></p>
<h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
<pre class="examples"><div class='input'><span class='co'>## Not run: ------------------------------------</span>
<span class='co'># # put some code like this on the clipboard</span>
<span class='co'># # (y <- 1:4)</span>
<span class='co'># # mean(y)</span>
<span class='co'># reprex()</span>
<span class='co'># </span>
<span class='co'># # provide code as an expression</span>
<span class='co'># reprex(rbinom(3, size = 10, prob = 0.5))</span>
<span class='co'># reprex({y <- 1:4; mean(y)})</span>
<span class='co'># </span>
<span class='co'># # note that you can include newlines in those brackets</span>
<span class='co'># # in fact, that is probably a good idea</span>
<span class='co'># reprex({</span>
<span class='co'># x <- 1:4</span>
<span class='co'># y <- 2:5</span>
<span class='co'># x + y</span>
<span class='co'># })</span>
<span class='co'># </span>
<span class='co'># ## provide code via character vector</span>
<span class='co'># reprex(input = c("x <- 1:4", "y <- 2:5", "x + y"))</span>
<span class='co'># </span>
<span class='co'># ## if just one line, terminate with '\n'</span>
<span class='co'># reprex(input = "rnorm(3)\n")</span>
<span class='co'># </span>
<span class='co'># ## customize the output comment prefix</span>
<span class='co'># reprex(rbinom(3, size = 10, prob = 0.5), comment = "#;-)")</span>
<span class='co'># </span>
<span class='co'># # override a default chunk option, in general</span>
<span class='co'># reprex({(y <- 1:4); median(y)}, opts_chunk = list(collapse = FALSE))</span>
<span class='co'># # the above is simply shorthand for this and produces same result</span>
<span class='co'># reprex({</span>
<span class='co'># #+ setup, include = FALSE</span>
<span class='co'># knitr::opts_chunk$set(collapse = FALSE)</span>
<span class='co'># </span>
<span class='co'># #+ actual-reprex-code</span>
<span class='co'># (y <- 1:4)</span>
<span class='co'># median(y)</span>
<span class='co'># })</span>
<span class='co'># </span>
<span class='co'># # add prose, use general markdown formatting</span>
<span class='co'># reprex({</span>
<span class='co'># #' # A Big Heading</span>
<span class='co'># #'</span>
<span class='co'># #' Look at my cute example. I love the</span>
<span class='co'># #' [reprex](https://github.com/jennybc/reprex#readme) package!</span>
<span class='co'># y <- 1:4</span>
<span class='co'># mean(y)</span>
<span class='co'># })</span>
<span class='co'># </span>
<span class='co'># # read reprex from file</span>
<span class='co'># writeLines(c("x <- 1:4", "mean(x)"), "foofy.R")</span>
<span class='co'># reprex(input = "foofy.R")</span>
<span class='co'># </span>
<span class='co'># # read from file and write to similarly-named outfiles</span>
<span class='co'># reprex(input = "foofy.R", outfile = NA)</span>
<span class='co'># list.files(pattern = "foofy")</span>
<span class='co'># file.remove(list.files(pattern = "foofy"))</span>
<span class='co'># </span>
<span class='co'># # write rendered reprex to file</span>
<span class='co'># reprex({</span>
<span class='co'># x <- 1:4</span>
<span class='co'># y <- 2:5</span>
<span class='co'># x + y</span>
<span class='co'># }, outfile = "foofy")</span>
<span class='co'># list.files(pattern = "foofy")</span>
<span class='co'># file.remove(list.files(pattern = "foofy"))</span>
<span class='co'># </span>
<span class='co'># # write reprex to file AND keep figure local too, i.e. don't post to imgur</span>
<span class='co'># reprex({</span>
<span class='co'># #' Some prose</span>
<span class='co'># ## regular comment</span>
<span class='co'># (x <- 1:4)</span>
<span class='co'># median(x)</span>
<span class='co'># plot(x)</span>
<span class='co'># }, outfile = "blarg", opts_knit = list(upload.fun = identity))</span>
<span class='co'># list.files(pattern = "blarg")</span>
<span class='co'># unlink(list.files(pattern = "blarg"), recursive = TRUE)</span>
<span class='co'># </span>
<span class='co'># ## target venue = StackOverflow</span>
<span class='co'># ## http://stackoverflow.com/editing-help</span>
<span class='co'># ret <- reprex({</span>
<span class='co'># x <- 1:4</span>
<span class='co'># y <- 2:5</span>
<span class='co'># x + y</span>
<span class='co'># }, venue = "so")</span>
<span class='co'># ret</span>
<span class='co'># </span>
<span class='co'># ## target venue = R, also good for email or Slack snippets</span>
<span class='co'># ret <- reprex({</span>
<span class='co'># x <- 1:4</span>
<span class='co'># y <- 2:5</span>
<span class='co'># x + y</span>
<span class='co'># }, venue = "R")</span>
<span class='co'># ret</span>
<span class='co'># </span>
<span class='co'># ## include prompt and don't comment the output</span>
<span class='co'># ## use this when you want to make your code hard to execute :)</span>
<span class='co'># reprex({</span>
<span class='co'># x <- 1:4</span>
<span class='co'># y <- 2:5</span>
<span class='co'># x + y</span>
<span class='co'># }, opts_chunk = list(comment = NA, prompt = TRUE))</span>
<span class='co'># </span>
<span class='co'># ## leading prompts are stripped from source</span>
<span class='co'># reprex(input = c("> x <- 1:3", "> median(x)"))</span>
<span class='co'>## ---------------------------------------------</span></div></pre>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
<h2>Contents</h2>
<ul class="nav nav-pills nav-stacked">
<li><a href="#arguments">Arguments</a></li>
<li><a href="#value">Value</a></li>
<li><a href="#details">Details</a></li>
<li><a href="#examples">Examples</a></li>
</ul>
</div>
</div>
<footer>
<div class="copyright">
<p>Developed by Jennifer Bryan, David Robinson.</p>
</div>
<div class="pkgdown">
<p>Site built with <a href="http://hadley.github.io/pkgdown/">pkgdown</a>.</p>
</div>
</footer>
</div>
</body>
</html>