QCBS R Workshop
Steve Vissault, Marie-Hélène Brice, David Beauchesne & Kevin Cazelles
install.packages(c("rmarkdown", "htmlwidgets", "bookdown", "blogdown", "DT", "leaflet", "rbokeh"))
R Markdown produces dynamic document in a variety of format.
rticles
)bookdown
)blogdown
)rmarkdown
and deploys it on the web03:00
knitr
executes the code and converts .Rmd
to .md
; Pandoc renders the .md
file to the output format you want.Metadata with options are defined in the header of the .Rmd
document as a set of key
and value
(YAML syntax).
---output: html_document---
---output:html_document: toc: true pdf_document: toc: false---
Options available depend on the output.
See ?html_document
, ?pdf_document
, ?word_document
author
, title
, date
Use ?html_document
from your R console to:
🧶 Knit to HTML to see the output.
If this was easy, try to embed the Rmd source code to download.
03:00
---output: html_document: toc: true toc_float: true theme: flatly df_print: kable code_download: true---We have done a brief overview of the document options (the shape), we now make anintroduction of the content
# Header 1## Header 2### Header 3#### Header 4*italic* or _italic_**bold**`code````rlibrary(tibble)data(iris)glimpse(as_tibble(iris))```
code
library(tibble)data(iris)glimpse(as_tibble(iris))
Headers help you to structure your Rmd Document
Lorem ipsum dolor sit amet,consecteturadipiscing elit.- Cras convallis purus.- Nunc faucibus.- Maecenas ipsum dolor.Nulla vehicula metus vel tortorvenenatis luctus.Etiam tempus sit amet ligulanec pretium. Aenean ultricesmassa sed pulvinar pulvinar.1. Duis aliquam commodo volutpat.1. Mauris ultrices.1. Aliquam eu erat.
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Nulla vehicula metus vel tortor venenatis luctus.

[Hulk vs Trump](https://media.giphy.com/media/MkGyW2cOH7uhO/giphy.gif)
| Time | Session | Topic ||:--------------|:-------:|---------:|| _left_ | _center_| _right_ || 01:00 - 01:50 | 1 | Anatomy || 01:50 - 02:00 | | *Break* || 02:00 - 02:45 | 2 | Tables || 02:45 - 03:00 | | *Break* |
Time | Session | Topic |
---|---|---|
left | center | right |
01:00 - 01:50 | 1 | Anatomy |
01:50 - 02:00 | Break | |
02:00 - 02:45 | 2 | Tables |
02:45 - 03:00 | Break |
:
specify the alignement.This text is written in markdown```{r}library(tibble)data(iris)head(iris)```
r
between brackets {}
, why is that?library(tibble)data(iris)head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 setosa## 2 4.9 3.0 1.4 0.2 setosa## 3 4.7 3.2 1.3 0.2 setosa## 4 4.6 3.1 1.5 0.2 setosa## 5 5.0 3.6 1.4 0.2 setosa## 6 5.4 3.9 1.7 0.4 setosa
library(ggplot2)data(iris)ggplot( data=iris, aes(x = Sepal.Length, y = Sepal.Width) ) +geom_point( aes(color=Species, shape=Species)) +xlab("Sepal Length") +ylab("Sepal Width") +ggtitle("Sepal Length-Width")
library(leaflet)leaflet(height=400, width=400) %>% addTiles() %>% addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R")
library(rbokeh)p <- figure() %>% ly_points(Sepal.Length, Sepal.Width, data = iris, color = Species, glyph = Species, hover = list(Sepal.Length, Sepal.Width))p
We studied `r
length(levels(iris$Species))`
iris species and took
measurements on `r
nrow(iris)`
flowers.
We found that Iris virginica had the longest sepal with a mean of
`r
mean(subset(iris,Species=="virginica")$Sepal.Length)`
millimeters!
This text is written in markdown.We studied 3 iris species and took measurements on 150 flowers.We found that Iris virginica had the longest sepal with a mean of 6.588 millimeters!
Place between curly braces
{r option=value}
Multiple options separated by commas
{r option1=value, option2=value}
Label your code chunk!
```{r dispIris, option1=value, option2=value}library(tibble)data(iris)head(iris)```
Chunk output can be customised with numerous options:
str(knitr::opts_chunk$get())
## List of 53## $ eval : logi TRUE## $ echo : logi TRUE## $ results : chr "markup"## $ tidy : logi FALSE## $ tidy.opts : NULL## $ collapse : logi FALSE## $ prompt : logi FALSE## $ comment : chr "##"## $ highlight : logi TRUE## $ strip.white : logi TRUE## $ size : chr "normalsize"## $ background : chr "#F7F7F7"## $ cache : logi FALSE## $ cache.path : chr "index_cache/html/"## $ cache.vars : NULL## $ cache.lazy : logi TRUE## $ dependson : NULL## $ autodep : logi FALSE## $ cache.rebuild: logi FALSE## $ fig.keep : chr "high"## $ fig.show : chr "asis"## $ fig.align : chr "default"## $ fig.path : chr "index_files/figure-html/"## $ dev : chr "png"## $ dev.args : NULL## $ dpi : num 72## $ fig.ext : NULL## $ fig.width : num 7## $ fig.height : num 7## $ fig.env : chr "figure"## $ fig.cap : NULL## $ fig.scap : NULL## $ fig.lp : chr "fig:"## $ fig.subcap : NULL## $ fig.pos : chr ""## $ out.width : NULL## $ out.height : NULL## $ out.extra : NULL## $ fig.retina : num 1## $ external : logi TRUE## $ sanitize : logi FALSE## $ interval : num 1## $ aniopts : chr "controls,loop"## $ warning : logi TRUE## $ error : logi FALSE## $ message : logi TRUE## $ render : NULL## $ ref.label : NULL## $ child : NULL## $ engine : chr "R"## $ split : logi FALSE## $ include : logi TRUE## $ purl : logi TRUE
```{r}head(iris)```
head(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 setosa## 2 4.9 3.0 1.4 0.2 setosa## 3 4.7 3.2 1.3 0.2 setosa## 4 4.6 3.1 1.5 0.2 setosa## 5 5.0 3.6 1.4 0.2 setosa## 6 5.4 3.9 1.7 0.4 setosa
echo
```{r echo = FALSE}head(iris)```
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species## 1 5.1 3.5 1.4 0.2 setosa## 2 4.9 3.0 1.4 0.2 setosa## 3 4.7 3.2 1.3 0.2 setosa## 4 4.6 3.1 1.5 0.2 setosa## 5 5.0 3.6 1.4 0.2 setosa## 6 5.4 3.9 1.7 0.4 setosa
echo = -1
to hide only the first line of a code chunk.eval
```{r eval = FALSE}head(iris)```
head(iris)
eval = -1
to evaluate every line of a code chunk except the first.include
```{r include = FALSE}head(iris)```
results
```{r results = "hold"}1 + 12 + 2```
1 + 12 + 2
## [1] 2## [1] 4
results
```{r results = "hide"}1 + 1ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width)) + geom_point()```
1 + 1ggplot(data = iris, aes(x = Petal.Length, y = Petal.Width)) + geom_point()
fig.height
& fig.width
```{r fig.height = 3, fig.width = 5, echo = FALSE}ggplot(data = iris, aes( x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()```
Option | Run code | Show code | Output | Plots | Messages | Warnings |
---|---|---|---|---|---|---|
eval = FALSE |
||||||
include = FALSE |
||||||
echo = FALSE |
||||||
results = "hide" |
||||||
fig.show = "hide" |
||||||
message = FALSE |
||||||
warning = FALSE |
Table from R for Data Science
You can change the default chunk options for all following chunks in your document.
```{r include = FALSE}knitr::opts_chunk$set( collapse = TRUE, cache = TRUE, comment = "#>", fig.width = 6, fig.align = "center")```
.Rmd
document and run data(diamonds)
within a chunkAdd the following text and fill it: We have data about XXXX diamonds. Only
XXXX are larger than 2.5 carats.
Display all diamonds larger than 2.5 carats with the function DT::datatable()
.
DT::datatable()
doesn't work with PDF or Word document, why?10:00
✔️ Document your document: use YAML to set up meaningful metadata
✔️ Style your document: use YAML to add options to your chosen output format
✔️ Organize your text: use markdown headers with #
✔️ Organize your code: use knitr
chunk labels
✔️ Style your text: use markdown bold, italics, bullets, and lists
✔️ Style your output: use knitr
chunk options
🧶 early, 🧶 often
Multiplateform | Portable | Reproducible
Multiplateform | Portable | Reproducible
Impress your director with dynamic output | Turn into a geek | Expose your skills on the web
Send an HTML output on Github
.Rmd
document and generate the HTML document (we know that part!)add
) and document (commit
) the modifications on the repository.Rmd
document and generate the HTML document (we know that part!)add
) and document (commit
) the modifications on the repositoryfirstOnlineDocument
Several options
In RStudio: file > new project... > Version control > Git
Repository URL
with the URL address of your repo and add .git
at the endhttps://github.com/SteveViss/firstOnlineDocument.git
.Rmd
document and generate the HTML document.Rmd
and produces a .html
file in the RStudio project folder.index.html
& index.Rmd
05:00
add
) and document (commit
) the modifications on the repositoryadd
) and document (commit
) the modifications on the repositoryWait few minutes and see the result at https://YOURUSERNAME.github.io/firstOnlineDocument/
down
universebookdown
thesisdown
rticles
posterdown
xaringan
or rmarkdown
vitae
blogdown
pkgdown
Specify the ioslides_presentation
output format in the YAML metadata of your document
---title: "My beautiful ioslide presentation"author: "John Doe"date: '2019-10-02'output: ioslides_presentation---
Knit 🧶 to create the HTML presentation!
Create new slides by using #
or ##
# Section slides | super stuff
Create new slides by using #
or ##
# Section slide | with background image{data-background=bg_mountain.jpg data-background-size=cover}
Create new slides by using #
or ##
## Slides with content- I love science- and **kitten!**{ width=60% }
## Slide with R code```{r, echo = TRUE}fit <- lm(dist ~ 1 + speed, data = cars)coef(summary(fit))```
## Slide with R plot```{r, echo = TRUE}plot(dist ~ speed, data = cars)```
---title: "My beautiful ioslide presentation"author: "John Doe"date: '2019-10-02'output: ioslides_presentation: logo: insilecoLogo.png---
widesreen
transition
incremental
smaller
---title: "My beautiful ioslide presentation"author: "John Doe"date: '2019-10-02'output: ioslides_presentation: widescreen: true transition: slower incremental: true smaller: true---
You can customize your presentation by adding your own CSS and your own template
---title: "My beautiful ioslide presentation"author: "John Doe"date: '2019-10-02'output: ioslides_presentation: css: mystyles.css template: mytemplate.html---
slidy_presentation
in the YAML---title: "My beautiful slidy presentation"author: "John Doe"date: '2019-10-02'output: slidy_presentation---
---title: "My beautiful PPT presentation"author: "John Doe"date: '2019-10-02'output: powerpoint_presentation---
.Rmd
files#
01-Introduction.Rmd
02-Chapter1.Rmd
03-Conclusion.Rmd
# IntroductionThis is the introduction
# Chapter 1This is the Chapter 1
# ConclusionThis is the conclusion
HTML
pdf
E-Books
down
universebookdown
thesisdown
rticles
posterdown
xaringan
or rmarkdown
vitae
blogdown
pkgdown
install.packages(c("rmarkdown", "htmlwidgets", "bookdown", "blogdown", "DT", "leaflet", "rbokeh"))
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 |