forked from rstudio/rmarkdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles_verbatim2.html
361 lines (286 loc) · 12 KB
/
articles_verbatim2.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Ramnath Vaidyanathan" />
<meta name="date" content="2014-03-12" />
<title>Verbatim Chunks in R Markdown</title>
<script src="site_libs/header-attrs-2.3.7/header-attrs.js"></script>
<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/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.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>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-20375833-3', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['rstudio.com', 'rstudio.github.io', 'rviews.rstudio.com', 'community.rstudio.com', 'rpubs.rstudio.com', 'environments.rstudio.com', 'rstudio.org', 'dailies.rstudio.com', 'pages.rstudio.com', 'db.rstudio.com', 'solutions.rstudio.com', 'docs.rstudio.com', 'spark.rstudio.com', 'shiny.rstudio.com', 'education.rstudio.com', 'rstudio.cloud', 'shinyapps.io', 'team-admin.rstudio.com', 'blog.rstudio.com', 'support.rstudio.com', 'blogs.rstudio.com', 'keras.rstudio.com', 'swag.rstudio.com', 'tensorflow.rstudio.com', 'packagemanager.rstudio.com'] );
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</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%;
}
.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;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<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">Verbatim Chunks in R Markdown</h1>
<h4 class="author">Ramnath Vaidyanathan</h4>
<h4 class="date">March 12, 2014</h4>
</div>
<p><em>This article is reposted from <a href="https://ramnathv.github.io/posts/verbatim-chunks-knitr/">Addicted to R</a> with the kind permission of Ramnath Vaidyanathan.</em></p>
<p><a href="https://www.rstudio.com/ide/docs/r_markdown">RMarkdown</a> has truly revolutinized the way I write. The simplicity of <a href="http://daringfireball.net/projects/markdown/">markdown</a> and the extreme flexibility of <a href="http://github.com/yihui/knitr">knitr</a> allows me to combine text and code in ways I would never have been able to do before.</p>
<p>One of the things I have always wanted to do is to be able to print code chunks verbatim, along with the chunk markup. This is especially useful while writing tutorials, where I want the user to see the code chunk “asis”.</p>
<p>Initially, I thought that I could achieve this by indenting the code chunk, thereby “fooling” <a href="http://github.com/yihui/knitr">knitr</a> into believing that it is not a code chunk, and thereby printing it verbatim in a <code><code></code> block. But I was terribly mistaken. Apparently, <code>knitr</code> is too smart to be fooled, and no matter how deep you tuck away or indent the chunk, <code>knitr</code> always finds it and executes it!</p>
<p>Digging a little deeper, I found an example from the author in his <a href="https://github.com/yihui/knitr-examples/blob/master/062-chunk-wrapper.Rmd">knitr-examples</a> repo. The basic idea was to write a <a href="yihui.name/knitr/hooks">chunk hook</a> that wraps the source code with the markup. It makes use of the option <code>params.src</code> that contains the chunk options that are set locally.</p>
<pre class="r"><code>knitr::knit_hooks$set(wrapper = function(before, options, envir) {
if (before) {
sprintf(' ```{r %s}\n', options$params.src)
} else ' ```\n'
})</code></pre>
<p>Let us test it out by adding <code>wrapper = TRUE</code> to a code chunk.</p>
<pre><code>```{r test_label, wrapper=TRUE, eval=FALSE}</code></pre>
<pre class="r"><code>1+1
plot(1)</code></pre>
<pre><code>```</code></pre>
<p>It works! Well, it almost works. I say almost, because I don’t like how it splits the code and the chunk markup into different blocks. I am sure that I can clean this up using some regex-fu, but I wanted to explore a cleaner solution using hooks.</p>
<p>After a lot of experimentation, I ended up with the following hook. Note that my implementation involves a source hook and not a chunk hook, like the one proposed by <a href="http://yihui.name">Yihui</a>. It is customized for <code>Rmd</code> documents, but is easy to extend for other formats like <code>Rnw</code>.</p>
<pre class="r"><code>knitr::knit_hooks$set(source = function(x, options){
if (!is.null(options$verbatim) && options$verbatim){
opts = gsub(",\\s*verbatim\\s*=\\s*TRUE\\s*", "", options$params.src)
bef = sprintf('\n\n ```{r %s}\n', opts, "\n")
stringr::str_c(
bef,
knitr:::indent_block(paste(x, collapse = '\n'), " "),
"\n ```\n"
)
} else {
stringr::str_c("\n\n```", tolower(options$engine), "\n",
paste(x, collapse = '\n'), "\n```\n\n"
)
}
})</code></pre>
<p>Let us now test how this hook performs.</p>
<pre><code>```{r qplot, fig.width = 5, fig.height = 5, fig.align = "center", message=FALSE}
library(ggplot2)
qplot(wt, am, color = vs, data = mtcars)
```</code></pre>
<p><img src="articles_verbatim2_files/figure-html/qplot-1.png" width="480" style="display: block; margin: auto;" /></p>
<p>Yeyy! It works exactly like how I wanted it to. As always, this post is fully reproducible, and you can find the code <a href="https://ramnathv.github.io/posts/verbatim-chunks-knitr/index.Rmd">here</a>.</p>
<style>
pre {
padding: 0;
}
pre code {
padding: 1rem;
}
p {
text-align: justify;
}
</style>
</div> <!-- articleBandContent -->
</div> <!-- pageContent -->
<div id="rStudioFooter" class="band full">
<div class="bandContent">
<div id="copyright">© Copyright 2016 - 2020 RStudio, PBC</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-pbc" class="footerLogo linkedIn"></a>
<a href="https://www.facebook.com/rstudioinc/" 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>
<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 -->
</body>
</html>