-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmarkdown_intro.Rmd
145 lines (108 loc) · 4.23 KB
/
markdown_intro.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
---
title: "Intro to Markdown"
subtitle: _writing repoducible reports_
author: "Matt Bixley"
date: "`r Sys.Date()`"
output:
html_document:
code_folding: show
df_print: kable
theme: simplex
highlight: textmate
toc: yes
toc_float: yes
number_sections: yes
bibliography: resources/markdown.bib
#csl: resources/new-zealand-veterinary-journal.csl
csl: resources/molecular-ecology.csl
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
#knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(kableExtra)
library(palmerpenguins)
library(knitr)
```
# R Markdown
## Objectives
- text formatting
- code
* highlighting
* chunks
* inline
- tables
- ploting
- citations
## Notes
Murray Cadzow wrote some notes (with markdown) for last years Webinar https://hackmd.io/@murraycadzow/nesi-rmarkdown-webinar#Figures
## Example
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using _R Markdown_ see <http://rmarkdown.rstudio.com>. [@rmarkdown2018]
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can highlight a piece of code like this: `Ctrl + Shft + I`
1 + 1 = `r 1 +1`
# Palmer Penguins
We are going to use the Palmer Penguins dataset curated by [Allison Horst](https://github.com/allisonhorst/palmerpenguins) and can be installed from Cran with the code `install.packages("palmerpenguins")` There are 3 different species of penguins in this dataset, collected from 3 islands in the Palmer Archipelago, Antarctica.
Our first *Code Chunk* will be to look at the dataset
```{r penguins}
head(penguins)
```
## Tables
Use the `kable()` function from `knitr` to make tables.
```{r count}
penguins %>%
count(species) %>%
kable(caption = "Species Counts")
```
We can generate more elegent tables with the [kableExtra](https://haozhu233.github.io/kableExtra/awesome_table_in_html.html#overview) package.
```{r kableextra}
penguins %>%
group_by(species) %>%
select(contains(c("length", "depth", "mass"))) %>%
summarize_all(., mean, na.rm = TRUE) %>%
kable(caption = "Summarized Data", format = "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = F, position = "left")
```
## Plots
You can also embed plots, for example:
```{r histogram, echo = T, fig.cap = "Caption for the plot created", fig.align = "center", warning = FALSE}
flipper_hist <- ggplot(data = penguins, aes(x = flipper_length_mm)) +
geom_histogram(aes(fill = species),
alpha = 0.5,
position = "identity") +
scale_fill_manual(values = c("darkorange","purple","cyan4")) +
theme_minimal() +
labs(x = "Flipper length (mm)",
y = "Frequency",
title = "Penguin flipper lengths")
flipper_hist
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
## Equations
equation $z = x + y$ can be written inline or equations can be centered in their own block
$$a^2 + b^2 = c^2$$
# More Options
## Images
If we have images or plots already stored that we wish to add we can write the following with or without an image caption.
```{r image, fig.align = "center", fig.width=5, fig.height=5, caption="Palmer Penguins"}
knitr::include_graphics("resources/pp_logo.png")
```
## Engines
Python, Shell, SQL, Rcpp, Stan, JavaScript, Julia, C and Fortran are all options
Bash
```{bash}
echo "Hello from Bash!"
```
Python
```{python}
def square(a):
return a * a
square(4)
```
All the available Engines can be found with the following line `names(knitr::knit_engines$get())`
`r names(knitr::knit_engines$get())`
## Citations
How do we cite things?
Citations need a **.bib** file and to know the citation key. We can cite PalmerPenguins [@horst2020] and at the same time lets give the citation for R created by Robert Gentleman and Ross Ihaka [@r2020] or inline like this @r2020
And we can change all the stlyes of citation to suit. [Here](https://citationstyles.org/) or here at [Zotero](https://www.zotero.org/styles)
# References