| 1 |
--- |
| 2 |
title: "knitr Reference Card" |
| 3 |
author: "Yihui Xie" |
| 4 |
date: "`{r} Sys.Date()`" |
| 5 |
output: |
| 6 |
html: |
| 7 |
meta: |
| 8 |
css: ["@default"] |
| 9 |
options: |
| 10 |
number_sections: true |
| 11 |
--- |
| 12 |
|
| 13 |
```{=html} |
| 14 |
<!-- |
| 15 |
%\VignetteEngine{litedown::vignette} |
| 16 |
%\VignetteIndexEntry{knitr Reference Card} |
| 17 |
--> |
| 18 |
``` |
| 19 |
|
| 20 |
## Syntax |
| 21 |
|
| 22 |
| format | start | end | inline | output | |
| 23 |
|-----------|---------------------|-----------------|--------------------|----------| |
| 24 |
| Rnw | `<<*>>=` | `@` | `\Sexpr{x}` | TeX | |
| 25 |
| Rmd | ```` ```{r *} ```` | ```` ``` ```` | `` `r x` `` | Markdown | |
| 26 |
| Rhtml | `<!--begin.rcode *` | `end.rcode-->` | `<!--rinline x-->` | HTML | |
| 27 |
| Rrst | `.. {r *}` | `.. ..` | `` :r:`x` `` | reST | |
| 28 |
| Rtex | `% begin.rcode *` | `% end.rcode` | `\rinline{x}` | TeX | |
| 29 |
| Rasciidoc | `// begin.rcode *` | `// end.rcode` | `+r x+` | AsciiDoc | |
| 30 |
| Rtextile | `### begin.rcode *` | `### end.rcode` | `@r x@` | Textile | |
| 31 |
| brew | | | `<% x %>` | text | |
| 32 |
|
| 33 |
`*` denotes local chunk options, e.g., `<<label, eval=FALSE>>=`; `x` denotes |
| 34 |
inline R code, e.g., `` `r 1+2` ``. |
| 35 |
|
| 36 |
## Minimal Examples |
| 37 |
|
| 38 |
::: col-2 |
| 39 |
### Sweave (\*.Rnw) |
| 40 |
|
| 41 |
``` tex |
| 42 |
\documentclass{article} |
| 43 |
\begin{document} |
| 44 |
|
| 45 |
Below is a code chunk. |
| 46 |
|
| 47 |
<<foo, echo=TRUE>>= |
| 48 |
z = 1 + 1 |
| 49 |
plot(cars) |
| 50 |
@ |
| 51 |
|
| 52 |
The value of z is \Sexpr{z}. |
| 53 |
\end{document} |
| 54 |
``` |
| 55 |
|
| 56 |
### R Markdown (\*.Rmd) |
| 57 |
|
| 58 |
```` md |
| 59 |
--- |
| 60 |
title: "An R Markdown document" |
| 61 |
--- |
| 62 |
|
| 63 |
Hi _Markdown_! |
| 64 |
|
| 65 |
```{r foo, echo=TRUE} |
| 66 |
z = 1 + 1 |
| 67 |
plot(cars) |
| 68 |
``` |
| 69 |
|
| 70 |
The value of z is `r z`. |
| 71 |
```` |
| 72 |
::: |
| 73 |
|
| 74 |
## Chunk Options |
| 75 |
|
| 76 |
`opts_chunk` controls global chunk options, e.g., |
| 77 |
`knitr::opts_chunk$set(tidy = FALSE)`, which can be overridden by local chunk |
| 78 |
options. See all options at <https://yihui.org/knitr/options/>. Some frequently |
| 79 |
used options are: |
| 80 |
|
| 81 |
- `eval`: whether to evaluate the chunk |
| 82 |
- `echo`: whether to echo source code |
| 83 |
- `results`: `'markup'`, `'asis'`, `'hold'`, `'hide'` |
| 84 |
- `tidy`: whether to reformat R code |
| 85 |
- `cache`: whether to cache results |
| 86 |
- `fig.width`, `fig.height`, `out.width`, `out.height`: device and output size |
| 87 |
of figures |
| 88 |
- `include`: whether to include the chunk results in output |
| 89 |
- `child`: path to child documents |
| 90 |
- `engine`: language name (R, python, ...) |
| 91 |
|
| 92 |
## Functions |
| 93 |
|
| 94 |
- `knit()`: the main function in this package; knit input document and write |
| 95 |
output |
| 96 |
- `purl()`: extract R code from an input document |
| 97 |
- `spin()`: spin goat's hair (an R script with roxygen comments) into wool (a |
| 98 |
literate programming document to be passed to `knit()`) |
| 99 |
- `stitch()`: insert an R script into a template and compile the document |
| 100 |
- `knit_hooks$set()`: set or reset chunk and output |
| 101 |
[hooks](https://yihui.org/knitr/hooks/) |
| 102 |
|
| 103 |
## Resources |
| 104 |
|
| 105 |
- Homepage: <https://yihui.org/knitr/> |
| 106 |
- Github: <https://github.com/yihui/knitr> |
| 107 |
([CRAN](https://cran.r-project.org/package=knitr)) |
| 108 |
- Examples: <https://github.com/yihui/knitr-examples> |
| 109 |
- Stack Overflow: <https://stackoverflow.com/tags/knitr/> |
| 110 |
|
| 111 |
```{css, echo=FALSE} |
| 112 |
body { |
| 113 |
max-width: none; |
| 114 |
} |
| 115 |
table { |
| 116 |
width: 100%; |
| 117 |
} |
| 118 |
.body h2 { |
| 119 |
border-bottom: none; |
| 120 |
font-size: 1.3em; |
| 121 |
} |
| 122 |
.body, .col-2 { |
| 123 |
columns: 2; |
| 124 |
& > :first-child { |
| 125 |
margin-top: 0; |
| 126 |
} |
| 127 |
} |
| 128 |
``` |