Presented by Emi Tanaka
School of Mathematics and Statistics
dr.emi.tanaka@gmail.com
@statsgen
4th October 2019 | COMBINE | Sydney, Australia
These slides are viewed best by Chrome and occasionally need to be refreshed if elements did not load properly. See here for PDF .
R Markdown integrates text + code
in one source document with ability to knit to many output formats (via Pandoc).
# Header 1## Header 2- Unordered list 1 - Unordered list 21. Ordered list 11. Ordered list 2_This is italic._ *This too.*__This is bold.__ **This too.**_**This is bold & italic.**_
Output
This is italic. This too. This is bold. This too. This is bold & italic.
In RStudio .Rmd press
to insert a chunk of R code
```{r}```
echo
& eval
```{r, echo = FALSE}plot(speed ~ dist, cars)```
```{r, eval = FALSE}plot(speed ~ dist, cars)```
plot(speed ~ dist, cars)
There are many more chunk options.
Can you name 5 other ones?
Hint: https://yihui.name/knitr/options/
(We'll explore some later.)
fig.path = figures/
is not valid but fig.path = "figures/"
is valideval = true
is not valid buteval = runif(1) > 0.5
is validThe chunk below is called plot1
.
```{r plot1}ggplot(cars, aes(dist, speed)) + geom_point()```
All chunks have a label regardless of whether it is explicitly supplied or not.
Do not include spaces, "_" or punctuation marks in your chunk name!
Today's date is `r Sys.Date()`.
Today's date is 2019-10-03.
The value of $\pi$ is `r pi`.
The value of π is 3.1415927.
echo
and always eval
uates.challenge-02.Rmd
challenge-03.Rmd
challenge-04.Rmd
challenge-05.Rmd
challenge-06.Rmd
25:00
```{python, echo = FALSE}a = [1, 2, 3]a[0]```
## 1
```{bash, echo = FALSE}date +%B```
## October
Basic format
---key: value---
Example
---title: "Communicating with Data via R Markdown"subtitle: "Reproducible Reports"author: "Emi Tanaka"date: "`r Sys.Date()`"output: html_document---
There must be a space after ":
"!
All YAML data are stored in rmarkdown::metadata
as list.
rmarkdown::metadata$title
## [1] "Communicating with Data via R markdown"
rmarkdown::metadata$author
## [1] "Emi Tanaka"
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta name="author" content="Emi Tanaka" /><meta name="date" content="2019-10-04" /><title>Communicating with Data via R Markdown</title></head><body><h1 class="title toc-ignore">Communicating with Data via R Markdown</h1><h3 class="subtitle">Reproducible Reports</h3><h4 class="author">Emi Tanaka</h4><h4 class="date">2019-10-04</h4></body></html>
html meta data
Default html template add special YAML key values to file automatically
#
.key
can hold multiple values. key: - value 1 - value 2
key: [value 1, value 2]
---title: "Communicating with Data via R Markdown"author: - "Emi Tanaka" - "Accomplice"output: html_document---
<body><h1 class="title toc-ignore">Communicating with Data via R Markdown</h1><h4 class="author">Emi Tanaka</h4><h4 class="author">Accomplice</h4> </body>
key
can contain key
s---output: html_document: toc: true toc_float: true---
What does this do?
(Note: white space is important)
---title: > this is a\ single line\abstract: | this value spans\ many lines and\ appears as it is\output: pdf_document---`r rmarkdown::metadata$title``r rmarkdown::metadata$abstract`
challenge-07.Rmd
10:00
---title: "Parameterized Report"params: species: setosaoutput: html_document---```{r, message = FALSE, fig.dim = c(3,2)}library(tidyverse)iris %>% filter(Species==params$species) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(aes(color=Species))```
---title: "Parameterized Report"params: species: label: "Species" value: setosa input: select choices: [setosa, versicolor, virginica] color: red max: label: "Maximum Sepal Width" value: 4 input: slider min: 4 max: 5 step: 0.1output: html_document---
```{r, message = params$printmsg, fig.dim = c(3,2)}library(tidyverse)iris %>% filter(Species==params$species) %>% filter(Sepal.Width < params$max) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(aes(color=Species), color = params$color)```
---title: "Parameterized Report"params: species: label: "Species" value: setosa input: select choices: [setosa, versicolor, virginica] color: red max: label: "Maximum Sepal Width" value: 5 input: slider min: 4 max: 5 step: 0.05output: html_document---
demo-render.Rmd
---title: "Parameterized Report"params: species: setosaoutput: html_document---```{r, message = FALSE, fig.dim = c(3,2)}library(tidyverse)iris %>% filter(Species==params$species) %>% ggplot(aes(Sepal.Length, Sepal.Width)) + geom_point(aes(color=Species))```
You can knit this file via R command by using render
function:
library(rmarkdown)render("demo-render.Rmd")
You can overwrite the YAML values by supplying arguments to render
:
library(rmarkdown)render("demo-render.Rmd", output_format = "pdf_document", params = list(species = "virginica"))
challenge-08.Rmd
and challenge-09.Rmd
10:00
html_document
You can change the look of the html document by specifying themes:
default
cerulean
journal
flatly
darkly
readable
spacelab
united
cosmo
lumen
paper
sandstone
simplex
yeti
NULL
output: html_document: theme: cerulean
These bootswatch themes attach the whole bootstrap library which makes your html file size larger.
prettydoc
prettydoc
📦 is a community contributed theme
that is light-weight:
cayman
tactile
architect
leonids
hpstr
rmdformats
rmdformats
📦 contains four built-in html
formats:
readthedown
html_clean
html_docco
material
You can use these formats by simply specifying the output in YAML as below:
output: rmdformats::readthedown
See more about it below:
rticles
- LaTeX Journal Article Templatesacm
acs
aea
agu
amq
ams
asa
biometrics
copernicus
elsevier
frontiers
ieee
jss
mdpi
mnras
peerj
plos
pnas
rjournal
rsos
rss
sage
sim
springer
tf
rticles
, each journal usually require external files (e.g. cls
or image files).GUI
RStudio > File > New File > R Markdown ... > From Template
Command line
rmarkdown::draft("file.Rmd", template = "biometrics_article", package = "rticles")
Default templates for many output are found at
We'll go through the latex template.
I found this nice latex template online.
You can see it at main.pdf
.
It was compiled from main.tex
.
main.tex
and main.pdf
in demo
folder.
How do I use this template so that I can write contents from an Rmd file instead?
We will use
---output: pdf_document: template: main.tex---But nothing written in the body shows up in the output!
We will use
---output: pdf_document: template: main.tex---But nothing written in the body shows up in the output!
You need to add $body$
in the latex template file where you want the body of the md file to appear.
\begin{document}
in latex template:\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}$if(highlighting-macros)$$highlighting-macros$$endif$
key
Rmd
---title: "COMBINE 2019"author: "Emi Tanaka"output: pdf_document: template: "template.tex"---
YAML meta data can be used by surrounding key
with $.
template.tex
\documentclass{article}\title{$title$}\author{$author$}\date{}\begin{document}\maketitle\end{document}
COMBINE 2019
Emi Tanaka
Rmd
---title: "COMBINE 2019"author: "Emi Tanaka"output: pdf_document: template: "template.tex"---
Simple "if null statements".
template.tex
\documentclass[$if(fontsize)$$fontsize$,$endif$]{article}\title{$title$}\author{$author$}\date{}\begin{document}\maketitle\end{document}
Rmd
---title: "COMBINE 2019"author: - name: "Rachel Wang" email: "rachel.wang@sydney.edu.au" - name: "Connor Smith" email: "connor.smith@sydney.edu.au"output: pdf_document: template: "template.tex"---
Here it will become
\author{Rachel Wang \and Connor Smith}
template.tex
\documentclass{article}\title{$title$}\author{$for(author)$$author.name$$sep$ \and $endfor$}\date{}\begin{document}\maketitle\end{document}
challenge-10.Rmd
05:00
# Some Header
an id is created automatically.-
and making it all lower case.[some text](#some-header)
.html
html
output, you can also give a link directly to the relevant section.demo-header.html
in the demo
folder in a web browser. #chicken-data
to the url. It should look like demo-header.html#chicken-data
{#your-id}
.# Some header {#header1}
header1
..bib
files.citation
function. (Scroll below to see the BibTeX citation).citation("xaringan")
## ## To cite package 'xaringan' in publications use:## ## Yihui Xie (2019). xaringan: Presentation Ninja. R package## version 0.9. https://CRAN.R-project.org/package=xaringan## ## A BibTeX entry for LaTeX users is## ## @Manual{,## title = {xaringan: Presentation Ninja},## author = {Yihui Xie},## year = {2019},## note = {R package version 0.9},## url = {https://CRAN.R-project.org/package=xaringan},## }
bib
file at YAML as:bibliography: bibliography.bib
[@bibtex-key] (Author et al. 2019)
or
@bibtex-key Author et al. 2019
demo-citation.Rmd
in the demo folder.R Markdown is such an indispensible tool for making documents, especially if you have plan to include statistical output.
How do you use (or plan to use)
R Markdown?
The development of R Markdown is largely thanks to
knitr
pandoc
R Markdown integrates text + code
in one source document with ability to knit to many output formats (via Pandoc).
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |