-
Notifications
You must be signed in to change notification settings - Fork 2
/
UsefulCode_rmarkdown.Rmd
329 lines (272 loc) · 7.72 KB
/
UsefulCode_rmarkdown.Rmd
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
---
title: Useful R code — `RMarkdown`
author: |
| Matthew Malishev
| @darwinanddavis
fontsize: 10
geometry: margin=1in
documentclass: article
linkcolor: pink
urlcolor: blue
citecolor: red
always_allow_html: yes
output:
html_document:
highlight: tango
code_folding: show
toc: yes
toc_depth: 4
number_sections: no
toc_float: yes
md_document:
variant: markdown_github
pdf_document:
includes:
in_header: # add .tex file with header content
highlight: tango
template: null
toc: yes
toc_depth: 4
number_sections: false
fig_width: 4
fig_height: 5
fig_caption: true
df_print: tibble
citation_package: biblatex # natbib
latex_engine: xelatex #pdflatex # lualatex
keep_tex: true # keep .tex file in dir
word_document:
highlight: tango
keep_md: yes
pandoc_args: --smart
#reference: mystyles.docx
toc: yes
toc_depth: 4
inludes:
before_body: before_body.tex
subtitle:
tags:
- nothing
- nothingness
params:
dir: "/Users/malishev/Documents/Melbourne Uni/Programs/R code/UsefulCode"
date: !r Sys.Date()
version: !r getRversion()
email: "matthew.malishev [at] gmail.com"
doi: https://github.com/darwinanddavis/UsefulCode
classoption: portrait
# ^['https://github.com/darwinanddavis/UsefulCode'] # footnote
vignette: >
%\VignetteIndexEntry{Useful R code}
%\VignetteEncoding{UTF-8}
%\VignetteEngine{knitr::rmarkdown}
---
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } });
</script>
```{r echo = FALSE}
library(rmarkdown)
# setwd("")
# f <- list.files()[1]
# render(f, output_format='pdf_document')
# render(f, output_format='pdf_document')
```
```{r, set-options, echo = F, cache = FALSE}
options(width=100)
knitr::opts_chunk$set(
eval = F, # run all code
results='hide',
echo = T, # show code chunks in output
comment = "",
tidy.opts=list(width.cutoff=100), # set width of code chunks in output
tidy=TRUE, # make output as tidy
message = FALSE, # mask all messages
warning = FALSE, # mask all warnings
size="small" # set code chunk size
)
# https://github.com/ucb-stat133/stat133-fall-2016/blob/master/hws/hw02-tables-ggplot.Rmd
knitr::opts_knit$set(root.dir=paste0(params$dir,"/")) # set working dir
setwd(paste0(params$dir,"/")) # for running just in R not knitr
```
\
Date: `r params$date`
`R` version: `r params$version`
*Corresponding author: `r params$email`
This document can be found at `r params$doi`
\newpage
## Overview
Useful functions and code snippets for building `RMarkdown` docs. _All `R` code is in Rmd file._
<!-- ———————————————————————————————————————————————————————————————————————————— -->
***
### Split page into three columns (displays best in browser).
_`R` code is in Rmd file._
_Check [Tokyo 2020 Olympics medal analysis](https://darwinanddavis.github.io/misc/olympics/olympics.html) for live example_
<!-- #https://holtzy.github.io/Pimp-my-rmd/ -->
<div class = "row">
<div class = "col-md-4">
```{r, rmarkdown1, message=F, echo=T, eval=T}
# r plot code
require(ggplot2)
ggplot(mtcars, aes(x=mpg)) +
geom_histogram(fill="skyblue", alpha=0.5) +
theme_classic()
```
</div>
<div class = "col-md-4">
<br><br>
>Praise the lord, I was born to travel
>Feeling like Slash in front of the chapel
>I'm leaned back with the Les Paul
>Shit I smoke is like cholesterol
>Spilled dressin' on the vest at the festival
>The best of all, had a midget Puerto Rican at my beckon call
</div>
<div class = "col-md-4">
<br><br>
Pump the bass in the trunk
It rattled like a baby hand
Except this toy cost 80 grand
And I'm crazy tan, from all the places that I've been
Just from writing words with a pen
</div>
<!-- close div -->
</div>
<!-- ———————————————————————————————————————————————————————————————————————————— -->
\
\
### Inserting text within indented equations
$$
p(x) = \theta^{x} (1 - \theta)^{1-x} ~~\mbox{ for }~~x = 0,1
$$
### Remove white space around imgs using `fig.asp`
`{r,fig.asp=0.50}`
### Read external Rmd chunks
<!-- https://stackoverflow.com/questions/52397430/include-code-from-an-external-r-script-run-in-display-both-code-and-output -->
**Option 1**
Add this to code chunk: `{r, code = readLines("file.R")}`
**Option 2**
<!-- live example in 'olympics.Rmd' -->
```{r, eval = F, message=F}
# load r datasets and code chunks
require(dplyr)
paste0(params$here) %>%
list.files("r",full.names = T) %>%
sapply(knitr::read_chunk)
# or
knitr::read_chunk('external.R')
```
Then in 'external.R' file, add the following header line:
`# @knitr external`
Then add this line to the code chunk in the main Rmd file:
`<<external>>`
### Knit an .R file script
<!-- https://twitter.com/jamespwr/status/1389916296171724803 -->
### Execute R code in yaml
```{yaml}
params:
date: !r Sys.Date()
email: Your email here
version: !r getRversion()
```
### Execute `R` code using yaml parameteres
Execute R code from yaml within HTML body
```{yaml}
params:
imgdir: "data/plots"
```
```{html}
# add to rmd
<img src=`r sample(list.files(params$imgdir, full.names = T),1)`>
```
### Change colour of tabs (pills)
```{html}
<!-- css for tabs -->
<style type="text/css">
.nav>li>a{
position: relative;
display: block;
padding: 10px 15px;
}
.nav-pills>li>a:hover{
background: `r params$primary`;
color: `r params$secondary`;
opacity:0.7;
}
.nav-pills>li>a:focus, .nav-pills>li.active>a, .nav-pills>li.active>a:hover, .nav-pills>li.active>a:focus {
background: `r params$primary`;
background-color: `r params$primary`;
}
</style>
```
```{markdown}
# add to rmd
# Your title {.tabset .tabset-fade .tabset-pills}
```
## Icons
### Feather icons
Add feather icons to Rmd
<!-- (see footer.html in Olympics example) -->
```{html}
<!-- for external html e.g. 'footer.html' -->
<!-- feather icons -->
<script src="https://unpkg.com/feather-icons"></script>
<!-- plot icon -->
<p style="text-align: center;">
<i data-feather="github"></i>
</p>
<!-- append to end of html file -->
<!-- feather icons -->
<script>
feather.replace()
</script>
```
```{html}
<!-- add to rmd -->
<center><i data-feather="chevron-down" class="arrow"></i></center>
```
## Pimping
### Add star ratings to HTML
<!-- https://tanho63.github.io/fivestars/ -->
```{r, stars, eval = T}
# install.packages("fivestars", repos = "https://tanho63.r-universe.dev")
library(fivestars)
# set css params
use_fivestars(
star_size = "15px", # default is "larger"
star_color = "#001785", # default
star_background = "#999" # default
)
# example 1
fivestars(rating = 3.5)
```
## Linking external files/code
Insert external HTML file within body
```{r}
htmltools::includeHTML("index_external.html")
```
Insert external HTML file in header, before body, and footer
```{yaml}
output:
html_document:
includes:
in_header: header.html
before_body: before.html
after_body: after.html
```
## CSS and custom styling
* Put in either 'styles.css' or in .Rmd file body
Change colour/style of crosstalk slider (`crosstalk::filter_slider()`)
```{css}
/* crosstalk style ---------------- */
.crosstalk-input-slider, .irs-grid-text .irs-text{
color: #5B3794;
font-family: Avernir;
}
.js-irs-0 .irs-single, .js-irs-0 .irs-bar-edge, .js-irs-0 .irs-bar {
color: #5B3794;
background: #5B3794;
border-top: 1px solid #5B3794;
border-bottom: 1px solid #5B3794;
background-color: #5B3794;
}
```