-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLesson5_rmd.Rmd
313 lines (249 loc) · 8.02 KB
/
Lesson5_rmd.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
---
title: Using RMarkdown for reproducible and neat documents
author: |
| Your name here
|
| Your affiliation here
output:
html_document: # html options
highlight: tango
code_folding: show
toc: yes
toc_depth: 4
number_sections: no
toc_float: yes
pdf_document: # pdf options
highlight: tango
template: null
toc: yes
toc_depth: 4
number_sections: false
fig_width: 4
fig_height: 5
fig_caption: true
df_print: tibble
latex_engine: xelatex #pdflatex # lualatex
word_document: # ms word options
highlight: tango
keep_md: yes
pandoc_args: --smart
toc: yes
md_document: # markdown options
variant: markdown_github
inludes:
before_body: before_body.tex
subtitle:
tags:
- nothing
- nothingness
params:
email: "your email here"
date: !r Sys.Date()
version: !r getRversion()
classoption: portrait
---
<!-- this enables math encoding in the knitted documents -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({ TeX: { equationNumbers: {autoNumber: "all"} } });
</script>
```{r, set-options, echo = F, cache = T,message=F}
options(width=100)
knitr::opts_chunk$set(
eval = TRUE, # run all code
echo = TRUE, # show 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
)
### run this once
# tinytex::install_tinytex()
packages <- c("dplyr","ggplot2","plotly","DT","htmlwidgets")
ppp <- lapply(packages,require,character.only=T)
names(ppp) <- packages
if(any(ppp==F)){cbind(packages,ppp);cat("\n\n\n ---> Check packages are loaded properly <--- \n\n\n")}
# knitr::opts_knit$set(root.dir=paste0(params$dir,"/")) # set working dir
# setwd(paste0(params$dir,"/")) # for running just in R not knitr
```
\newpage
Date: `r params$date`
R version: `r params$version`
*Corresponding author: `r params$email`
\newpage
# Overview
This document showcases how to create and use `RMarkdown` documents.
You can write in **bold** and *italicised* text (in _two_ different __ways__).
You can write in-line `code` if you want to differentiate between when you are typing normally or highlighting `model parameters`, for example.
Equations like this $t' = \gamma(t - vx/c^{2})$, to appear within text lines.
Create links to your [website](https://github.com/darwinanddavis).
Make footnotes^[Here is the footnote you created earlier, automatically formatted].
# You can easily create headings. This is the same font size as the 'Overview' above
## Then move down in font size
### Like this subheading
#### And this fourth order heading
Insert line breaks <br> between text like this
Insert a horizontal line break using five asterisks ('*****')
*****
(There is also a page break here. Best seen in PDF. Check the raw Rmd file to see the code)
The raw Rmd file also has the code for inserting user comments,
<!-- To do this, place the cursor on the text and type 'Cmd/Ctl + Shift + C' -->
\newpage
## Define equations
Accordingly, we write the eigenfunction of a spinless particle as the superposition of plane wave states of momentum ($\pi$) and energy ($Ej$) having amplitudes $a(\pi,Ej)$ (from^[Efthimiades, S., Physical meaning and derivation of Schrodinger and Dirac equations, Department of Natural Sciences, Fordham University. [doi: d34464566](https://arxiv.org/vc/quant-ph/papers/0607/0607001v1.pdf).])
$$
\phi n(r,t) =
\sum_{i, j} a(p_{i},E_{j})
e^{
\frac{i}
{h}
(p_{i} \cdot r - E_{j}t)
}
$$
\
## Embed images/gifs:
.
\newpage
## Create, alter, and embed plots
```{r,echo=F,eval=T}
set.seed(12)
N <- 2000
rr <- rnorm(N)
plot(rr,pch=20,main="Some random data")
```
Figure 1. Example of a stock plot embedded into a PDF from RMarkdown.
\newpage
## Show plots with associated code
```{r,echo=T,eval=T}
require(viridis)
bm <- 0
par(las=1,bty="n"); xlim <- c(-5,5);ylim <- c(0,0.5)
set.seed(12)
N <- 2000
rr <- rnorm(N); rr2 <- rnorm(N^2); rr3 <- rnorm(N+0.3)
rrd <- density(rr);rrd2 <- density(rr2);rrd3 <- density(rr3)
main <- paste0(N," points but plot better");xlab <- "Points in space"
if(bm==1){
layout(matrix(c(rep(1,3),2:4), 2, 3, byrow = TRUE));sc <- 1
plot(rr,las=1,bty="n",col=adjustcolor(viridis(N),0.5),pch=20,cex=runif(10,1,5),
main=main,xlab=xlab)
for(r in list(rrd,rrd2,rrd3)){
plot(r,xlim=xlim,ylim=ylim,main="")
polygon(r,col=adjustcolor(viridis(250)[sc],0.5),border=viridis(250)[sc]);sc <- sc+100}
}else{par(mfrow=c(1,1))
plot(rr,las=1,bty="n",col=adjustcolor(viridis(N),0.5),pch=20,cex=runif(10,1,5),
main=main,xlab=xlab)}
```
Figure 2. Example of a plot with improved graphics and its associated code embedded into a PDF from RMarkdown.
\newpage
## And tables
Table 1. Definitions of model parameters for individual hosts and **parasites**. Dimensions and units: -, dimensionless; cm, centimetres; J, Joules; L, length.
| Parameter | Definition | Dimension<br/>(unit) |
| :---: | :--- | ---: |
| _L_ | structural length | cm |
| _ee_ | scaled reserve density | J (cm^3^) |
| _D_ | host development | --- |
| _RH_ | energy in reproduction buffer| J |
\newpage
## Use buttons or tabs for sub-chapters {.tabset .tabset-fade .tabset-pills}
### Chapter 1
*Then you can add whatever you want here* like you would normally write in the `Rmd` file.
\
### Chapter 2 (with new code)
Here's an Easter egg for you ...
```{r,echo=F,message=F}
lapply(list(require(plotly),require(ggplot2)),suppressWarnings)
nn <- 100
p <- ggplot() +
geom_point(aes(rnorm(nn),sample(nn/2,nn,replace=T)),colour = rainbow(nn),size=sample(nn/2,nn,replace=T)/5,alpha=abs(rnorm(nn))) +
labs(x = "Random normally distributed data",
y = "Sampled integers") +
theme_minimal()
ggplotly(p)
```
### More tables
Here's a new way of creating tables using the `DT` package
```{r,cache=T,warning=F, echo=F}
lapply(list(require(DT),require(htmlwidgets)),suppressWarnings)
caption <- "Table 1. The 'mtcars' dataset presented in a datatable. Try editing the columns and cells."
datatable(mtcars,
rownames = T,
filter="top",
options = list(pageLength = 5, scrollX=T),
class = "cell-border stripe",
editable = "cell",
caption = caption
)
```
***
## Embed code from different languages
### This is `R` code
```{r, eval=F}
if(pck==1){
p<-c("rJava", "RNetLogo"); remove.packages(p)
# then install rJava and RNetLogo from source
install.packages("rJava", repos = "https://cran.r-project.org/")
install.packages("RNetLogo", repos = "https://cran.r-project.org/")
}
```
### `shell/bash`
```{bash, eval=F}
echo "Hello Bash!"
pwd # check working dir
git init # initialise git
```
### Octave (and MATLAB from the `RMatlab` package).
[`RMatlab documentation`](https://cran.r-project.org/web/packages/R.matlab/index.html).
```{octave, eval=F}
b = [4; 9; 2] # Column vector
A = [ 3 4 5;
1 3 1;
3 5 9 ]
x = A \ b # Solve the system Ax = b
```
### HTML
```{html, eval=F}
<!-- links-->
<div class="footer">
<a href="dd_feed.html"
class="transition fade_in">
Latest post
</a>
<a href="dd_contact.html"
class="transition fade_in">
Contact
</a>
<a href="dd_subscribe.html"
class="transition fade_in">
Subscribe
</a>
</div>
```
### CSS
```{css, eval=F}
# custom code for the tabs in this file
.btn {
border-width: 0 0px 0px 0px;
font-weight: normal;
text-transform: ;
}
.btn-default {
color: #f08080;
background-color: #ffffff;
border-color: #ffffff;
}
```
### Javascript to access `html` and `css`
```{js, eval=F}
$('.title').css('color', 'red')
```
### Python
```{python, eval=F}
x = 'hello, python world!'
print(x.split(' '))
```
### Here's a complete list of available languages
```{r eval=T}
names(knitr::knit_engines$get())
```