Processing math: 100%
+ - 0:00:00
Notes for current slide
Notes for next slide


R Markdown


Florian Privé & Julyan Arbel

April 11, 2019



Slides: bit.ly/RUGgre18


(adapted with permission from Antoine Bichat's presentation)

1 / 30

R Markdown

2 / 30

R Markdown?


  • Markdown is a lightweight markup language with plain text formatting syntax that can be converted to many formats, including HTML.
    It is completely independent from R.
    The extention is typically .md.
3 / 30

R Markdown?


  • Markdown is a lightweight markup language with plain text formatting syntax that can be converted to many formats, including HTML.
    It is completely independent from R.
    The extention is typically .md.

  • R Markdown is an extension of the markdown syntax that enables R code to be executed. The extention is typically .Rmd.

3 / 30

R Markdown?


  • Markdown is a lightweight markup language with plain text formatting syntax that can be converted to many formats, including HTML.
    It is completely independent from R.
    The extention is typically .md.

  • R Markdown is an extension of the markdown syntax that enables R code to be executed. The extention is typically .Rmd.

  • {rmarkdown} is an R package that proceses and converts .Rmd files into a number of different formats, including HTML, PDF and Word.

3 / 30

Create a new .Rmd in RStudio

4 / 30

Create a new .Rmd in RStudio

4 / 30

New .Rmd

5 / 30

Compile .Rmd

Use the Knit button to produce a HTML file


Shortcut: Ctrl + Shift + K

6 / 30

Markdown syntax

7 / 30

Titles and text fonts in Markdown

# Big title
## Title
### Small title
_Italic_, *italic*, __bold__, **bold**, and `monospace`
8 / 30

Titles and text fonts in Markdown

# Big title
## Title
### Small title
_Italic_, *italic*, __bold__, **bold**, and `monospace`

Big title

Title

Small title

Italic, italic, bold, bold, and monospace

8 / 30

Lists in Markdown

1. This is
2. an ordered
3. list
  1. This is

  2. an ordered

  3. list

9 / 30

Lists in Markdown

1. This is
2. an ordered
3. list
  1. This is

  2. an ordered

  3. list

* This is
* a bullet list
* with indent
  • This is

  • a bullet list

    • with indent
9 / 30

Lists in Markdown

1. This is
2. an ordered
3. list
  1. This is

  2. an ordered

  3. list

* This is
* a bullet list
* with indent
  • This is

  • a bullet list

    • with indent


  • You can enumerate only with '1.'; it will increment automatically

  • You can also use '-' or '+' instead of '*'

9 / 30

More in Markdown

> You can insert quotes and $\LaTeX$ expressions

You can insert quotes and LATEX expressions

10 / 30

More in Markdown

> You can insert quotes and $\LaTeX$ expressions

You can insert quotes and LATEX expressions


$$ \frac{1}{n} \sum_{i=1}^n X_i $$

1nni=1Xi

10 / 30

More in Markdown

> You can insert quotes and $\LaTeX$ expressions

You can insert quotes and LATEX expressions


$$ \frac{1}{n} \sum_{i=1}^n X_i $$

1nni=1Xi


  • Make an horizontal line with '***'

  • Add 4 spaces at the end of your text in order to write to the next line

  • You can write HTML in Markdown (e.g. use '<br>' to get a blank line)

10 / 30
[Antoine's personal page](https://abichat.github.io/)

Antoine's personal page

11 / 30
[Antoine's personal page](https://abichat.github.io/)

Antoine's personal page

![](https://slides.yihui.name/gif/impossible-parking.gif)

11 / 30
[Antoine's personal page](https://abichat.github.io/)

Antoine's personal page

![](https://slides.yihui.name/gif/impossible-parking.gif)

It also works directly with HTML syntax:

<a href="https://abichat.github.io/">Antoine's personal page</a>
<img src="https://slides.yihui.name/gif/impossible-parking.gif">
11 / 30

Code chunks

12 / 30

Basic chunk

```{r}
x <- 4
x
```
13 / 30

Basic chunk

```{r}
x <- 4
x
```
[1] 4
13 / 30

Basic chunk

```{r}
x <- 4
x
```
[1] 4

Echo

To display the output of a code chunk but not the underlying R code, you specify the echo=FALSE option

```{r, echo=FALSE}
x <- 5
x
```
13 / 30

Basic chunk

```{r}
x <- 4
x
```
[1] 4

Echo

To display the output of a code chunk but not the underlying R code, you specify the echo=FALSE option

```{r, echo=FALSE}
x <- 5
x
```

Eval

To display R code without evaluating it, you specify the eval=FALSE chunk option

```{r, eval=FALSE}
x <- 5
```
13 / 30

Figure options (1/2)

```{r, fig.align="center", out.width="60%"}
ggplot2::qplot(Sepal.Length, Sepal.Width, data = iris)
```

14 / 30

Figure options (2/2)

```{r, fig.align="center", out.width="80%", fig.asp=0.7, fig.width=4}
ggplot2::qplot(Sepal.Length, Sepal.Width, data = iris)
```

15 / 30

A better way to include pictures

```{r, fig.align="center", out.width="80%", echo=FALSE}
knitr::include_graphics(
"https://slides.yihui.name/gif/impossible-parking.gif")
```

It allows you to use the chunk options for image size.

16 / 30

FAQ: two figures side-by-side

```{r, echo=FALSE, out.width="50%", fig.show="hold", fig.align="default"}
plot(iris)
plot(iris, pch = 20, col = iris$Species)
```

17 / 30

Options and Formats

18 / 30

Supported languages in chunks

  • R

  • Python

  • Shell

  • SQL

  • Rcpp

  • Stan

  • JavaScript

  • CSS

  • Julia

  • C and Fortran

  • many others...

19 / 30

Data frame printing and tables

You can play with YAML option df_print. For example, df_print: paged:

iris ## or directly with `DT::datatable(iris)`
ABCDEFGHIJ0123456789
Sepal.Length
<dbl>
Sepal.Width
<dbl>
Petal.Length
<dbl>
Petal.Width
<dbl>
Species
<fct>
5.13.51.40.2setosa
4.93.01.40.2setosa
4.73.21.30.2setosa
4.63.11.50.2setosa
5.03.61.40.2setosa
5.43.91.70.4setosa
4.63.41.40.3setosa
5.03.41.50.2setosa
4.42.91.40.2setosa
4.93.11.50.1setosa

For more refined tables, you can use knitr::kable(), xtable::xtable(), kableExtra::kable().

20 / 30

Use R Markdown for..


  • Reports (analysis, etc) with text, code and results in the same place! With many possible output formats including HTML, PDF, MS Word, etc.

  • Notebooks (vs "Chunk Output in Console")

  • Slides (like this one! -- see source code)

  • Websites (such as the website of our R user group)

  • Books (e.g. my online advanced R course or even a thesis)

  • [WIP] {pagedown}: Paginate the HTML Output of R Markdown with CSS for Print

21 / 30

Slides formats

22 / 30

Slides formats


3 main reasons to make your slides with R Markdown:

22 / 30

Slides formats


3 main reasons to make your slides with R Markdown:

  1. Include code and results very easily
22 / 30

Slides formats


3 main reasons to make your slides with R Markdown:

  1. Include code and results very easily

  2. Make HTML presentations directly available online (easy to export them to PDF using "print to PDF" of Chrome)

22 / 30

Slides formats


3 main reasons to make your slides with R Markdown:

  1. Include code and results very easily

  2. Make HTML presentations directly available online (easy to export them to PDF using "print to PDF" of Chrome)

  3. Include fancy HTML widgets (like maps, searchable tables, interactive graphics, etc)

22 / 30

Include an interactive map with {leaflet}

library(leaflet)
leaflet(width = "100%") %>%
setView(lng = 5.767249, lat = 45.190590, zoom = 12) %>%
addTiles(options = providerTileOptions(minZoom = 2, maxZoom = 19))
23 / 30

Websites


Example: webpage of the Grenoble RUG


How does it work?

  • just multiple Rmd documents (one by page) rendered as HTML with a common navigation bar

  • host the static HTML pages using e.g. GitHub pages

  • make your own website in 20 minutes with this tutorial

You can also use {blogdown}. See the full documentation.

24 / 30

Books


Every R book is now written with {bookdown}.

See many of them at https://bookdown.org/.


Again, from our side, it is just a collection of R Markdown documents.

E.g. see my advanced R course and the associated GitHub repository.

25 / 30

Technical Documents


Formats in {bookdown} allows you to:

  • number and reference equations, tables, figures, etc

  • use special chunks such as theorems and proofs

  • special headers such as "PART" and "APPENDIX"

  • citations

  • etc

26 / 30

Technical Documents


Formats in {bookdown} allows you to:

  • number and reference equations, tables, figures, etc

  • use special chunks such as theorems and proofs

  • special headers such as "PART" and "APPENDIX"

  • citations

  • etc


To use this in single R Markdown documents (not books), you can use formats

  • bookdown::html_document2()
  • bookdown::pdf_document2()
  • bookdown::word_document2()
26 / 30

References

27 / 30

28 / 30

Thanks!


Slides: bit.ly/RUGgre18


privefl      privefl      F. Privé

JulyanArbel      jarbel

Slides created via the R package xaringan.

30 / 30

R Markdown

2 / 30
Paused

Help

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