-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR_rmarkdown_reference.R
202 lines (176 loc) · 4.4 KB
/
R_rmarkdown_reference.R
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
#' ---
#' title: "rmarkdown::render snippets"
#' subtitle: "R code snippets for scripts enhanced with rmarkdown `#'` markup"
#' author: "Lorenzo Gaborini"
#' date: ""
#' output:
#' html_document:
#' toc: true
#' toc_float:
#' collapsed: false
#' self_contained: true
#' ---
#' \newcommand{\tuple}[1]{\langle #1 \rangle}
#+ include=FALSE
knitr::opts_chunk$set(clean = FALSE)
knitr::opts_knit$set(progress = FALSE)
#' # Setup chunk options
#'
library(knitr)
#' Remove knitr progress bar during page build:
#+ eval=FALSE
knitr::opts_knit$set(progress = FALSE)
#' Enable knitr cache:
#+ eval=FALSE
knitr::opts_chunk$set(cache = TRUE,
clean = FALSE,
autodep = TRUE,
cache.comments = FALSE,
cache.path = 'cache/')
#' Clear knitr cache:
#+ eval=FALSE
knitr::opts_chunk$set(cache.rebuild = TRUE)
#' Auto pretty print:
#+ eval=FALSE
library(printr)
#' # Chunks without R comments
#'
#' rmarkdown::render supports echoing R code used to build a chunks.
#' R comments can be suppressed in echoed code using chunk option `tidy`.
#'
#' ## Within global options
#'
#' Change all chunks in the current document:
#'
#+ eval=FALSE
knitr::opts_chunk$set(tidy = TRUE, tidy.opts = list(comment = FALSE))
#' ## Using a chunk template
#'
#' Change only selected chunks.
#'
#' Within global options, we can specify a chunk template:
knitr::opts_template$set(nocomments = list(tidy = TRUE, tidy.opts = list(comment = FALSE)))
#' Template usage: set `opts.label` for a new chunk
#' `#+ opts.label="nocomments"`
#'
#' ## Results
#'
#' Without template:
#+ eval=FALSE
choose.method <- 'ML'
# choose.method <- 'fixed'
# choose.method <- 'exact'
#' With template:
#'
#+ opts.label="nocomments"
choose.method <- 'ML'
# choose.method <- 'fixed'
# choose.method <- 'exact'
#' # pandoc markdown extensions
#'
#' `pandoc` markdown extensions are supported: see [reference](http://rmarkdown.rstudio.com/authoring_pandoc_markdown.html#pandoc_markdown).
#'
#' ## TeX's `newcommand`
#'
#' Set just after YAML header!
#'
#' `\newcommand{\tuple}[1]{\langle #1 \rangle}`
#'
#' `$\tuple{a, b, c}$` inserts: $\tuple{a, b, c}$
#'
#'
#' # R matrices to $\LaTeX$
#'
#' This function converts an R matrix to LaTeX inline code:
bmatrix <- function(x, digits = NULL, display = TRUE, pre = '', post = '', ...) {
library(xtable)
default_args = list(
include.colnames = FALSE,
only.contents = TRUE,
include.rownames = FALSE,
hline.after = NULL,
comment = FALSE,
print.results = FALSE
)
passed_args = list(...)
calling_args = c(list(x = xtable(x, digits = digits)),
c(passed_args,
default_args[setdiff(names(default_args), names(passed_args))]))
if (display){
pre.tag <- '$$'
post.tag <- '$$'
} else {
pre.tag <- ''
post.tag <- ''
}
return(cat(
pre.tag,
pre,
"\\begin{bmatrix}\n",
do.call(print.xtable, calling_args),
"\\end{bmatrix}",
post,
post.tag
))
}
#' The call:
#' must use chunk option `results='asis'`
#+results='asis'
bmatrix(diag(2))
#' ## Adding a prefix to the line
#'
#' ### The wrong ways
#'
#' With TeX prefix on separate line:
#' $A =$
#+results='asis', echo=FALSE
bmatrix(diag(2))
#' With TeX displaymath prefix on separate line:
#' $$A =$$
#+results='asis', echo=FALSE
bmatrix(diag(2))
#' Inline equations
#' **not working**
#' `r bmatrix(diag(2))`
#'
#' Inline equations 2:
#' inlinemath **not working**
#' $`r bmatrix(diag(2))`$
#'
#' Inline equations 3:
#' displaymath **not working**
#' $$`r bmatrix(diag(2))`$$
#'
#' ### The correct way
#'
#' Using `bmatrix` with prefix, and `+results='asis'`:
#+results='asis'
bmatrix(diag(2), pre='A =')
#' ### Constant matrices
#'
#' We can inline $\LaTeX$ constant matrices using pure $\LaTeX$ markup:
#'
#' $$\mathbf{X} = \left[\begin{array}
#' {rrr}
#' 1 & 2 & 3 \\
#' 4 & 5 & 6 \\
#' 7 & 8 & 9
#' \end{array}\right]
#' $$
#'
#' # Completely ignore rmarkdown chunks
#'
#' Sometimes `include=FALSE` is not sufficient, one would like to omit sections from the resulting report.
#' It is possible to ignore entire chunks and markdown text (i.e. lines starting with `#'`).
#'
#' Just add C++ style comments:
1
#' Below, an ignored markdown line with R code:
#'
# /*
#' ignored markdown line
2
# */
#' Above, ignored markdown chunk:
#'
3