| 1 |
#LyX 2.0 created this file. For more info see http://www.lyx.org/ |
| 2 |
\lyxformat 413 |
| 3 |
\begin_document |
| 4 |
\begin_header |
| 5 |
\textclass article |
| 6 |
\use_default_options true |
| 7 |
\begin_modules |
| 8 |
knitr |
| 9 |
\end_modules |
| 10 |
\maintain_unincluded_children false |
| 11 |
\language english |
| 12 |
\language_package none |
| 13 |
\inputencoding default |
| 14 |
\fontencoding global |
| 15 |
\font_roman palatino |
| 16 |
\font_sans default |
| 17 |
\font_typewriter default |
| 18 |
\font_default_family default |
| 19 |
\use_non_tex_fonts false |
| 20 |
\font_sc true |
| 21 |
\font_osf false |
| 22 |
\font_sf_scale 100 |
| 23 |
\font_tt_scale 100 |
| 24 |
|
| 25 |
\graphics default |
| 26 |
\default_output_format default |
| 27 |
\output_sync 0 |
| 28 |
\bibtex_command default |
| 29 |
\index_command default |
| 30 |
\paperfontsize default |
| 31 |
\spacing single |
| 32 |
\use_hyperref true |
| 33 |
\pdf_bookmarks true |
| 34 |
\pdf_bookmarksnumbered true |
| 35 |
\pdf_bookmarksopen true |
| 36 |
\pdf_bookmarksopenlevel 2 |
| 37 |
\pdf_breaklinks false |
| 38 |
\pdf_pdfborder false |
| 39 |
\pdf_colorlinks false |
| 40 |
\pdf_backref false |
| 41 |
\pdf_pdfusetitle true |
| 42 |
\pdf_quoted_options "pdfstartview={XYZ null null 1}" |
| 43 |
\papersize default |
| 44 |
\use_geometry true |
| 45 |
\use_amsmath 1 |
| 46 |
\use_esint 1 |
| 47 |
\use_mhchem 1 |
| 48 |
\use_mathdots 1 |
| 49 |
\cite_engine basic |
| 50 |
\use_bibtopic false |
| 51 |
\use_indices false |
| 52 |
\paperorientation portrait |
| 53 |
\suppress_date false |
| 54 |
\use_refstyle 1 |
| 55 |
\index Index |
| 56 |
\shortcut idx |
| 57 |
\color #008000 |
| 58 |
\end_index |
| 59 |
\leftmargin 2.5cm |
| 60 |
\topmargin 2.5cm |
| 61 |
\rightmargin 2.5cm |
| 62 |
\bottommargin 2.5cm |
| 63 |
\secnumdepth 3 |
| 64 |
\tocdepth 3 |
| 65 |
\paragraph_separation indent |
| 66 |
\paragraph_indentation default |
| 67 |
\quotes_language english |
| 68 |
\papercolumns 1 |
| 69 |
\papersides 1 |
| 70 |
\paperpagestyle default |
| 71 |
\listings_params "language=R" |
| 72 |
\tracking_changes false |
| 73 |
\output_changes false |
| 74 |
\html_math_output 0 |
| 75 |
\html_css_as_file 0 |
| 76 |
\html_be_strict false |
| 77 |
\end_header |
| 78 |
|
| 79 |
\begin_body |
| 80 |
|
| 81 |
\begin_layout Standard |
| 82 |
\begin_inset ERT |
| 83 |
status open |
| 84 |
|
| 85 |
\begin_layout Plain Layout |
| 86 |
|
| 87 |
<<setup, include=FALSE, cache=FALSE>>= |
| 88 |
\end_layout |
| 89 |
|
| 90 |
\begin_layout Plain Layout |
| 91 |
|
| 92 |
library(knitr) |
| 93 |
\end_layout |
| 94 |
|
| 95 |
\begin_layout Plain Layout |
| 96 |
|
| 97 |
opts_chunk$set(fig.path = 'figure/listings-') |
| 98 |
\end_layout |
| 99 |
|
| 100 |
\begin_layout Plain Layout |
| 101 |
|
| 102 |
options(formatR.arrow = TRUE) |
| 103 |
\end_layout |
| 104 |
|
| 105 |
\begin_layout Plain Layout |
| 106 |
|
| 107 |
render_listings() |
| 108 |
\end_layout |
| 109 |
|
| 110 |
\begin_layout Plain Layout |
| 111 |
|
| 112 |
@ |
| 113 |
\end_layout |
| 114 |
|
| 115 |
\end_inset |
| 116 |
|
| 117 |
|
| 118 |
\end_layout |
| 119 |
|
| 120 |
\begin_layout Title |
| 121 |
Using listings with knitr |
| 122 |
\end_layout |
| 123 |
|
| 124 |
\begin_layout Author |
| 125 |
Yihui Xie |
| 126 |
\end_layout |
| 127 |
|
| 128 |
\begin_layout Standard |
| 129 |
To use the |
| 130 |
\series bold |
| 131 |
listings |
| 132 |
\series default |
| 133 |
package with |
| 134 |
\series bold |
| 135 |
knitr |
| 136 |
\series default |
| 137 |
, all you need to do is to call a function in your first setup chunk (that |
| 138 |
chunk should be hidden from the output with |
| 139 |
\family typewriter |
| 140 |
include=FALSE |
| 141 |
\family default |
| 142 |
and should not be cached): |
| 143 |
\end_layout |
| 144 |
|
| 145 |
\begin_layout Standard |
| 146 |
\begin_inset ERT |
| 147 |
status open |
| 148 |
|
| 149 |
\begin_layout Plain Layout |
| 150 |
|
| 151 |
% I just want to echo the 2nd line |
| 152 |
\end_layout |
| 153 |
|
| 154 |
\begin_layout Plain Layout |
| 155 |
|
| 156 |
<<setup, echo=2>>= |
| 157 |
\end_layout |
| 158 |
|
| 159 |
\begin_layout Plain Layout |
| 160 |
|
| 161 |
@ |
| 162 |
\end_layout |
| 163 |
|
| 164 |
\end_inset |
| 165 |
|
| 166 |
|
| 167 |
\end_layout |
| 168 |
|
| 169 |
\begin_layout Standard |
| 170 |
This function modifies the output hooks and header information so that the |
| 171 |
output is written in |
| 172 |
\series bold |
| 173 |
listings |
| 174 |
\series default |
| 175 |
environments, which are kindly provided by Frank Harrell and can be found |
| 176 |
in |
| 177 |
\begin_inset Flex URL |
| 178 |
status collapsed |
| 179 |
|
| 180 |
\begin_layout Plain Layout |
| 181 |
|
| 182 |
https://github.com/yihui/knitr/blob/master/inst/misc/Sweavel.sty |
| 183 |
\end_layout |
| 184 |
|
| 185 |
\end_inset |
| 186 |
|
| 187 |
. |
| 188 |
Of course you have to install the |
| 189 |
\series bold |
| 190 |
listings |
| 191 |
\series default |
| 192 |
package if your LaTeX toolset does not include this package. |
| 193 |
\end_layout |
| 194 |
|
| 195 |
\begin_layout Standard |
| 196 |
Now we can see how the results look like with our new settings: |
| 197 |
\end_layout |
| 198 |
|
| 199 |
\begin_layout Standard |
| 200 |
\begin_inset ERT |
| 201 |
status open |
| 202 |
|
| 203 |
\begin_layout Plain Layout |
| 204 |
|
| 205 |
<<boring-random>>= |
| 206 |
\end_layout |
| 207 |
|
| 208 |
\begin_layout Plain Layout |
| 209 |
|
| 210 |
set.seed(1121) # for reproducibility |
| 211 |
\end_layout |
| 212 |
|
| 213 |
\begin_layout Plain Layout |
| 214 |
|
| 215 |
options(width=85) |
| 216 |
\end_layout |
| 217 |
|
| 218 |
\begin_layout Plain Layout |
| 219 |
|
| 220 |
x=rnorm(20) |
| 221 |
\end_layout |
| 222 |
|
| 223 |
\begin_layout Plain Layout |
| 224 |
|
| 225 |
x |
| 226 |
\end_layout |
| 227 |
|
| 228 |
\begin_layout Plain Layout |
| 229 |
|
| 230 |
mean(x) |
| 231 |
\end_layout |
| 232 |
|
| 233 |
\begin_layout Plain Layout |
| 234 |
|
| 235 |
sqrt(-1) # this will give you a warning message |
| 236 |
\end_layout |
| 237 |
|
| 238 |
\begin_layout Plain Layout |
| 239 |
|
| 240 |
@ |
| 241 |
\end_layout |
| 242 |
|
| 243 |
\end_inset |
| 244 |
|
| 245 |
|
| 246 |
\end_layout |
| 247 |
|
| 248 |
\begin_layout Standard |
| 249 |
Another chunk: |
| 250 |
\end_layout |
| 251 |
|
| 252 |
\begin_layout Standard |
| 253 |
\begin_inset ERT |
| 254 |
status open |
| 255 |
|
| 256 |
\begin_layout Plain Layout |
| 257 |
|
| 258 |
<<rgl-hook-demo,eval=FALSE>>= |
| 259 |
\end_layout |
| 260 |
|
| 261 |
\begin_layout Plain Layout |
| 262 |
|
| 263 |
hook_rgl = function(before, options, envir) { |
| 264 |
\end_layout |
| 265 |
|
| 266 |
\begin_layout Plain Layout |
| 267 |
|
| 268 |
## after a chunk has been evaluated |
| 269 |
\end_layout |
| 270 |
|
| 271 |
\begin_layout Plain Layout |
| 272 |
|
| 273 |
if (before || !require('rgl') || rgl.cur() == 0) return() # no active |
| 274 |
device |
| 275 |
\end_layout |
| 276 |
|
| 277 |
\begin_layout Plain Layout |
| 278 |
|
| 279 |
name = paste(valid_prefix(options$fig.path), options$label, sep = '') |
| 280 |
\end_layout |
| 281 |
|
| 282 |
\begin_layout Plain Layout |
| 283 |
|
| 284 |
par3d(windowRect = 100 + options$dpi * c(0, 0, options$width, options$height |
| 285 |
)) |
| 286 |
\end_layout |
| 287 |
|
| 288 |
\begin_layout Plain Layout |
| 289 |
|
| 290 |
Sys.sleep(.05) # need time to respond to window size change |
| 291 |
\end_layout |
| 292 |
|
| 293 |
\begin_layout Plain Layout |
| 294 |
|
| 295 |
\end_layout |
| 296 |
|
| 297 |
\begin_layout Plain Layout |
| 298 |
|
| 299 |
fmt = opts_knit$get('out.format') |
| 300 |
\end_layout |
| 301 |
|
| 302 |
\begin_layout Plain Layout |
| 303 |
|
| 304 |
if (fmt %in% c('html', 'markdown', 'gfm', 'jekyll')) options$dev = 'png' |
| 305 |
\end_layout |
| 306 |
|
| 307 |
\begin_layout Plain Layout |
| 308 |
|
| 309 |
\end_layout |
| 310 |
|
| 311 |
\begin_layout Plain Layout |
| 312 |
|
| 313 |
## support 3 formats: eps, pdf and png (default) |
| 314 |
\end_layout |
| 315 |
|
| 316 |
\begin_layout Plain Layout |
| 317 |
|
| 318 |
switch(options$dev, |
| 319 |
\end_layout |
| 320 |
|
| 321 |
\begin_layout Plain Layout |
| 322 |
|
| 323 |
postscript = rgl.postscript(paste(name, '.eps', sep = ''), fmt |
| 324 |
= 'eps'), |
| 325 |
\end_layout |
| 326 |
|
| 327 |
\begin_layout Plain Layout |
| 328 |
|
| 329 |
pdf = rgl.postscript(paste(name, '.pdf', sep = ''), fmt = 'pdf'), |
| 330 |
\end_layout |
| 331 |
|
| 332 |
\begin_layout Plain Layout |
| 333 |
|
| 334 |
rgl.snapshot(paste(name, '.png', sep = ''), fmt = 'png')) |
| 335 |
\end_layout |
| 336 |
|
| 337 |
\begin_layout Plain Layout |
| 338 |
|
| 339 |
\end_layout |
| 340 |
|
| 341 |
\begin_layout Plain Layout |
| 342 |
|
| 343 |
if (fmt == 'html') return(.plot.hook.html(c(name, 'png'), options)) |
| 344 |
\end_layout |
| 345 |
|
| 346 |
\begin_layout Plain Layout |
| 347 |
|
| 348 |
if (fmt %in% c('markdown', 'gfm', 'jekyll')) |
| 349 |
\end_layout |
| 350 |
|
| 351 |
\begin_layout Plain Layout |
| 352 |
|
| 353 |
return(.plot.hook.markdown(c(name, 'png'), options)) |
| 354 |
\end_layout |
| 355 |
|
| 356 |
\begin_layout Plain Layout |
| 357 |
|
| 358 |
\end_layout |
| 359 |
|
| 360 |
\begin_layout Plain Layout |
| 361 |
|
| 362 |
paste(ifelse(options$align == 'center', ' |
| 363 |
\backslash |
| 364 |
|
| 365 |
\backslash |
| 366 |
centering{}', ''), ' |
| 367 |
\backslash |
| 368 |
|
| 369 |
\backslash |
| 370 |
includegraphics[', |
| 371 |
\end_layout |
| 372 |
|
| 373 |
\begin_layout Plain Layout |
| 374 |
|
| 375 |
sprintf('width=%s', options$out.width), ']{', name, '} |
| 376 |
\backslash |
| 377 |
n', sep = '') |
| 378 |
\end_layout |
| 379 |
|
| 380 |
\begin_layout Plain Layout |
| 381 |
|
| 382 |
} |
| 383 |
\end_layout |
| 384 |
|
| 385 |
\begin_layout Plain Layout |
| 386 |
|
| 387 |
@ |
| 388 |
\end_layout |
| 389 |
|
| 390 |
\end_inset |
| 391 |
|
| 392 |
|
| 393 |
\end_layout |
| 394 |
|
| 395 |
\begin_layout Standard |
| 396 |
Well, we can do crazier things with |
| 397 |
\series bold |
| 398 |
knitr |
| 399 |
\series default |
| 400 |
. |
| 401 |
Here we use the |
| 402 |
\family typewriter |
| 403 |
fig.cap |
| 404 |
\family default |
| 405 |
option to write plots into the |
| 406 |
\family typewriter |
| 407 |
figure |
| 408 |
\family default |
| 409 |
environment automatically, and the caption is generated from data dynamically |
| 410 |
(see Figure |
| 411 |
\begin_inset ERT |
| 412 |
status collapsed |
| 413 |
|
| 414 |
\begin_layout Plain Layout |
| 415 |
|
| 416 |
|
| 417 |
\backslash |
| 418 |
ref{fig:boxplot-ex} |
| 419 |
\end_layout |
| 420 |
|
| 421 |
\end_inset |
| 422 |
|
| 423 |
): |
| 424 |
\end_layout |
| 425 |
|
| 426 |
\begin_layout Standard |
| 427 |
\begin_inset ERT |
| 428 |
status open |
| 429 |
|
| 430 |
\begin_layout Plain Layout |
| 431 |
|
| 432 |
% set an option first |
| 433 |
\end_layout |
| 434 |
|
| 435 |
\begin_layout Plain Layout |
| 436 |
|
| 437 |
<<eval-opts, echo=FALSE>>= |
| 438 |
\end_layout |
| 439 |
|
| 440 |
\begin_layout Plain Layout |
| 441 |
|
| 442 |
opts_knit$set(eval.after = 'fig.cap') # evaluate fig.cap after the chunk |
| 443 |
\end_layout |
| 444 |
|
| 445 |
\begin_layout Plain Layout |
| 446 |
|
| 447 |
\end_layout |
| 448 |
|
| 449 |
\begin_layout Plain Layout |
| 450 |
|
| 451 |
<<boxplot-ex, fig.height=2, out.width='.8 |
| 452 |
\backslash |
| 453 |
|
| 454 |
\backslash |
| 455 |
linewidth', fig.cap=sprintf('This is a boxplot; the median is %.2f.', median(x))>>= |
| 456 |
\end_layout |
| 457 |
|
| 458 |
\begin_layout Plain Layout |
| 459 |
|
| 460 |
par(mar=c(4,4,.1,.1)) |
| 461 |
\end_layout |
| 462 |
|
| 463 |
\begin_layout Plain Layout |
| 464 |
|
| 465 |
boxplot(x, horizontal=TRUE) |
| 466 |
\end_layout |
| 467 |
|
| 468 |
\begin_layout Plain Layout |
| 469 |
|
| 470 |
@ |
| 471 |
\end_layout |
| 472 |
|
| 473 |
\end_inset |
| 474 |
|
| 475 |
|
| 476 |
\end_layout |
| 477 |
|
| 478 |
\begin_layout Standard |
| 479 |
As we know, |
| 480 |
\family typewriter |
| 481 |
figure |
| 482 |
\family default |
| 483 |
is a float environment, so it has floated away from the R code to the top |
| 484 |
of this page. |
| 485 |
This should not be surprising to LaTeX users. |
| 486 |
\end_layout |
| 487 |
|
| 488 |
\begin_layout Standard |
| 489 |
You should be able to compile the TeX document and get a PDF file like this |
| 490 |
one: |
| 491 |
\begin_inset Flex URL |
| 492 |
status collapsed |
| 493 |
|
| 494 |
\begin_layout Plain Layout |
| 495 |
|
| 496 |
https://github.com/yihui/knitr/releases/download/doc/knitr-listings.pdf |
| 497 |
\end_layout |
| 498 |
|
| 499 |
\end_inset |
| 500 |
|
| 501 |
. |
| 502 |
For more information about out hooks, see |
| 503 |
\begin_inset Flex URL |
| 504 |
status collapsed |
| 505 |
|
| 506 |
\begin_layout Plain Layout |
| 507 |
|
| 508 |
https://yihui.org/knitr/hooks/ |
| 509 |
\end_layout |
| 510 |
|
| 511 |
\end_inset |
| 512 |
|
| 513 |
. |
| 514 |
\end_layout |
| 515 |
|
| 516 |
\end_body |
| 517 |
\end_document |