-
Notifications
You must be signed in to change notification settings - Fork 1
/
Lesson5_rmd.Rmd
359 lines (285 loc) · 9.47 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
---
title: "Using RMarkdown for reproducible and neat documents"
author: |
| Your name here
| Your affiliation here
params:
date: !r Sys.Date()
email: Your email here
version: !r getRversion()
output:
pdf_document:
toc: yes
toc_depth: '4'
includes:
in_header: # add .tex file with header content
highlight: tango
template: null
number_sections: false
fig_width: 4
fig_height: 5
fig_caption: true
df_print: tibble
# citation_package: biblatex # natbib
latex_engine: pdflatex #lualatex #xelatex #pdflatex
keep_tex: false # keep .tex file in dir
html_document:
code_folding: show
highlight: tango
number_sections: no
toc: yes
toc_depth: 4
toc_float: yes
tags:
classoption: article
linkcolor: blue
urlcolor: blue
---
<!-- 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=25,tinytex.verbose = TRUE, width.cutoff=25)
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,
tidy.opts=list(width.cutoff=25) # set width of code chunks in output
)
### run this once
# tinytex::install_tinytex()
# require(tinytex)
# install.packages("pacman")
# require(pacman)
suppressWarnings(pacman::p_load(dplyr,ggplot2,plotly,DT,htmlwidgets,viridis,reticulate))
# update.packages("htmlwidgets")
### updating latex pcks (june 2020)
# update.packages(ask = FALSE, checkBuilt = TRUE)
# tinytex::tlmgr_update() # update LaTeX packages
# tinytex::reinstall_tinytex() # if lazy load corrupt error, restart r and try again
# 'Lesson5_rmd.md' file overrides PDF rendering, so check this file for possible typos/errors
# render by command line
# for word
# rmarkdown::render("Lesson5_rmd.Rmd", output_format = "word_document")
# for pdf after setting eval=T in Embed imgs/pdf code chunk
# rmarkdown::render("Lesson5_rmd.Rmd", output_format = "pdf_document")
# ------ potential solution for text wrapping in code chunks for PDF
# set text/limits (N) within code chunks using str_wrap(width=N)
# rep("String with trailing,
# middle, and leading white space\t String with trailing,
# middle, and leading white space\t ",10) %>%
# stringr::str_wrap(width=10)
```
\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 easily create headings. This is a first order heading.
## Then move down in heading order size
### Like this subheading
#### And this fourth order heading
<!-- To create a new line of text, insert two spaces after every line in the `Rmd` file, then hit _ENTER_. -->
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].
Insert line breaks <br> between text like this, which works best in large <br> slabs of text
Insert a horizontal line break using five asterisks ('*****')
<!-- horizontal line -->
<!-- ***** -->
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' -->
(There is also a page break here. Best seen in PDF. Check the raw Rmd file to see the code)
<!-- page break -->
\newpage
## Define equations
Accordingly, we write the eigenfunction of a spinless particle as the superposition of plane wave states of momentum ($\pi$) and energy ($E_{j}$) having amplitudes $a(\pi,E_{j})$ (from [[1]](https://www.academia.edu/33763545/Concise_derivation_of_the_Schr%C3%B6dinger_equation)).
$$
\phi n(r,t) =
\sum_{i, j} a(p_{i},E_{j})
e^{
\frac{i}
{h}
(p_{i} \cdot r - E_{j}t)
}
$$
\
<!-- !!! uncheck the below to include images in HTML and word renders -->
<!-- !!! pdf render code is found in the below commmit -->
<!-- ## Embed images/gifs: -->
<!-- ![Great power is pretty boring](https://raw.githubusercontent.com/darwinanddavis/EmoRyCodingClub/master/img/opm.jpg). -->
<!-- code for img in pdf: commit L5 rmd external test final - cmd line pdf/word knit -->
\newpage
## Create, alter, and embed plots
```{r,echo=F,eval=T}
par(mar=rep(1,4))
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,message=F,width.cutoff=25}
suppressWarnings(require(viridis))
bm <- 1
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 the output document 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}
suppressWarnings(pacman::p_load(plotly,ggplot2))
set.seed(12) # set a number seed for reproducible number results
nn <- 100
random_data <- data.frame("X" = rnorm(nn),
"Y" = sample(nn/2,nn,replace=T),
"NN" = nn)
p <- ggplot() +
geom_point(data=random_data,aes(X,Y,
colour=rainbow(nn),
size=sample(Y)/5),
alpha=abs(rnorm(nn))) +
labs(x = "Random normally distributed data",
y = "Sampled integers") +
theme_minimal() +
theme(legend.position='none')
ggplotly(p,tooltip=c("X","Y"))
```
### More tables
Here's a new way of creating tables using the `DT` package
```{r,cache=T,warning=F, echo=F}
suppressWarnings(pacman::p_load(DT,htmlwidgets))
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}
pck <- 0
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())
```
# References
[1] Efthimiades, S., Physical meaning and derivation of Schrodinger and Dirac equations, Department of Natural Sciences, Fordham University, [doi: d34464566](https://www.academia.edu/33763545/Concise_derivation_of_the_Schr%C3%B6dinger_equation).