-
Notifications
You must be signed in to change notification settings - Fork 0
/
html20190810.html
504 lines (434 loc) · 14.8 KB
/
html20190810.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style type="text/css">
.inline {
background-color: #f7f7f7;
border:solid 1px #B0B0B0;
}
.error {
font-weight: bold;
color: #FF0000;
}
.warning {
font-weight: bold;
}
.message {
font-style: italic;
}
.source, .output, .warning, .error, .message {
padding: 0 1em;
border:solid 1px #F7F7F7;
}
.source {
background-color: #f5f5f5;
}
.left {
text-align: left;
}
.right {
text-align: right;
}
.center {
text-align: center;
}
.hl.num {
color: #AF0F91;
}
.hl.str {
color: #317ECC;
}
.hl.com {
color: #AD95AF;
font-style: italic;
}
.hl.opt {
color: #000000;
}
.hl.std {
color: #585858;
}
.hl.kwa {
color: #295F94;
font-weight: bold;
}
.hl.kwb {
color: #B05A65;
}
.hl.kwc {
color: #55aa55;
}
.hl.kwd {
color: #BC5A65;
font-weight: bold;
}
</style>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Setting Output args via Render functions</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<meta name="description" content="Turn your analyses into high quality documents, reports, presentations and dashboards with R Markdown. Use a productive notebook interface to weave together narrative text and code to produce elegantly formatted output. Use multiple languages including R, Python, and SQL. R Markdown supports a reproducible workflow for dozens of static and dynamic output formats including HTML, PDF, MS Word, Beamer, HTML5 slides, Tufte-style handouts, books, dashboards, shiny applications, scientific articles, websites, and more." />
<link rel="icon" type="image/png" href="images/favicon.png" />
<script type="text/javascript" src="js/redirect.js"></script>
<script type="text/javascript" src="js/rmarkdown.js"></script>
<script type="text/javascript" src="js/ga.js"></script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="css/rmarkdown.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div id="rStudioHeader" class="alwaysShrunk">
<div class="innards bandContent">
<div>
<a class="productName" href="index.html">R Markdown</a>
<div class="rStudio"><span class="light">from </span> <a href="https://www.rstudio.com/"><div class="logo"></div></a></div>
</div>
<div id="menu">
<div id="menuToggler"></div>
<div id="menuItems">
<a class="menuItem" href="lesson-1.html">Get Started</a>
<a class="menuItem" href="gallery.html">Gallery</a>
<a class="menuItem" href="formats.html">Formats</a>
<a class="menuItem" href="articles.html">Articles</a>
<a class="menuItem" href="https://bookdown.org/yihui/rmarkdown/">Book</a>
<a class="menuItem" href="docs/">References</a>
<a class="menuItem gitHub" href="https://github.com/rstudio/rmarkdown"></a>
<a class="menuItem gitHubText" href="https://github.com/rstudio/rmarkdown">Source on GitHub</a>
</div>
</div>
</div>
</div>
<style type="text/css">
#TOC {
margin-left: 35px;
margin-top: 90px;
}
</style>
<script type="text/javascript">
$(".main-container").removeClass("main-container").removeClass("container-fluid").addClass("footerPushDown");
</script>
<div id="pageContent" class="standardPadding">
<div class="articleBandContent">
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Setting Output args via Render functions</h1>
</div>
<div id="basic-usage" class="section level2">
<h2>Basic Usage</h2>
<p>Often in an interactive R Markdown document, you want to embed snippets of Shiny code without having to specify the corresponding <code>ui</code> function. For example, you may want to use a <code>renderPlot()</code> function without having to create a <code>plotOutput()</code> slot beforehand. In this case, Shiny helpfully associates the corresponding output object to each <code>render</code> function, letting you use Shiny code outside of a full app. However, some functionality is lost in this process. In particular, <code>plotOutput()</code> can take in some optional arguments to set things like width and height, or allow you to click or brush over the plot (and store that information). Previously, this functionality was not available to you outside of a full Shiny app (one with a <code>server</code> function and a <code>ui</code> function, where all the <code>output</code> objects are made explicit).</p>
<p>Now, for the sake of convenience, when working with just snippets of Shiny code, you’ll able to set <code>output</code> arguments directly from the corresponding <code>render</code> function. To do so, use the <code>outputArgs</code> argument, which is available to all <code>render</code> functions. For example, let’s say that you want to render a plot and specify its width to be 200px and its height 100px. Then you should use:</p>
<pre class="markdown"><code>```{r, echo = FALSE}
renderPlot({
plot(yourData)
}, outputArgs = list(width = "200px",
height = "100px")
)
```
</code></pre>
<p>No matter how many arguments you set (all the way from zero to all possible ones), <code>outputArgs</code> always takes in a list (as you’d expect, the default is an empty list, which sets no <code>output</code> arguments). If you try to pass in a non-existent argument, you’ll get an error with the following message (in this example, you tried to set an argument named “not_an_argument”):</p>
<p style="color:#f00; font-family: serif; padding: 12px">
<strong>Error:</strong> Unused argument: in ‘outputArgs’, ‘not_an_argument’ is not an valid argument for the output function
</p>
<p>To see this in action, check out the R Markdown script and resulting document below. The document is interactive – brush over the image and see the ‘xmin’, ‘xmax’, ‘ymin’ and ‘ymax’ values change (printed just under the image).</p>
<div id="script" class="section level3">
<h3>Script</h3>
<pre class="markdown"><code>---
title: Setting `output` args via `render` functions
runtime: shiny
output: html_document
---
This interactive Rmd document makes use of the `outputArgs` argument now available to all Shiny `render` functions. To give an example, this allows you to set arguments to `imageOutput` through `renderImage`. This means that you don't have to create a `ui` object just to be able to brush over an image. Note that this only applies to snippets of Shiny code during an interactive Rmd (and not to embedded full apps -- the ones you need to call `shinyApp` to run).
### Brushing over an image (and storing the data)
```{r setup, echo=FALSE}
library(datasets)
generateImage <- function() {
outfile <- tempfile(fileext = '.png')
png(outfile)
par(mar = c(0,0,0,0))
image(volcano, axes = FALSE)
contour(volcano, add = TRUE)
dev.off()
list(src = outfile)
}
```
```{r image}
renderImage({
generateImage()
}, deleteFile = TRUE,
outputArgs = list(brush = brushOpts(id = "plot_brush"),
width = "250",
height = "250px")
)
```
##### Here's some of the brushing info sent to the server:
(brush over the image to change the data)
```{r brush info}
renderText({
print(input$plot_brush)
brush <- input$plot_brush
paste0("xmin: ", brush$xmin, "; ",
"xmax: ", brush$xmax, "; ",
"ymin: ", brush$ymin, "; ",
"ymax: ", brush$ymax)
})
```
---
### Resizing a plot
```{r plot}
renderPlot({
plot(cars)
}, outputArgs = list(width = "75%",
height = "250px")
)
```
</code></pre>
<p><br></p>
</div>
<div id="result" class="section level3">
<h3>Result</h3>
<iframe src="https://gallery.shinyapps.io/output-args/" frameborder="no" class="scale65" height="1360px" style="margin-bottom: -480px;">
</iframe>
</div>
</div>
<div id="a-caveat" class="section level2">
<h2>A caveat</h2>
<p>It’s important to highlight that you can only use this functionality within an interactive R Markdown document (i.e. you must set <code>runtime: shiny</code> in the header). But even if that is the case, this is only applicable to <code>ui</code>-less pieces of Shiny code. If you embed a full Shiny application in your document and try to use <code>outputArgs</code>, it will be ignored and print the following warning to the R Markdown console (in this case, your <code>ui</code> function would be something like <code>ui <- plotOutput("plot")</code>):</p>
<p style="color:#f00; font-family: serif; padding: 12px">
Warning in <code>output$plot</code>(…) :<br> Unused argument: outputArgs. The argument outputArgs is only meant to be used when embedding snippets of Shiny code in an R Markdown code chunk (using runtime: shiny). When running a full Shiny app, please set the output arguments directly in the corresponding output function of your UI code.
</p>
<p>The same will happen if you try to use <code>outputArgs</code> in any other context, like inside a regular (i.e. not embedded) Shiny app. The rationale is that, if you are already specifying a <code>ui</code> function with all the <code>output</code> objects made explicit, you should set their arguments directly there, instead of going through this round-about way.</p>
</div>
</div> <!-- articleBandContent -->
</div> <!-- pageContent -->
<div id="rStudioFooter" class="band full">
<div class="bandContent">
<div id="copyright">© Copyright 2016 - 2018 RStudio Inc.</div>
<div id="logos">
<a href="https://twitter.com/rstudio" class="footerLogo twitter"></a>
<a href="https://github.com/rstudio" class="footerLogo gitHub"></a>
<a href="https://www.linkedin.com/company/rstudio-inc" class="footerLogo linkedIn"></a>
<a href="https://www.facebook.com/pages/RStudio-Inc/267733656584415" Class="footerLogo facebook"></a>
</div>
</div>
</div>
<!-- bizible -->
<script type="text/javascript" src="//cdn.bizible.com/scripts/bizible.js" async=""></script>
<!-- disqus -->
<div id="disqus_thread" class="standardPadding"></div>
<script type="text/javascript" src="js/disqus.js"></script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<script type="text/javascript" src="js/external-links.js"></script>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = false;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
</body>
</html>