+ - 0:00:00
Notes for current slide
Notes for next slide

Fundamentals of Data Science for EESS





R session 02 - R markdown

Daniel Vaulot

2021-01-20

1 / 35

Outline



  • What is Markown ?
  • Rmarkdown syntax
  • R chunks
  • Some applications
2 / 35

What is markdown ?

  • Created in 2004 by John Gruber and Aaron Swartz

  • Goal : "to write using an easy-to-read and easy-to-write plain text format, optionally convert it to structurally valid HTML".

3 / 35

What is markdown ?

  • Created in 2004 by John Gruber and Aaron Swartz

  • Goal : "to write using an easy-to-read and easy-to-write plain text format, optionally convert it to structurally valid HTML".

Many flavors...

  • MultiMarkdown
  • GitHub Flavored Markdown (GFM)
  • Pandoc
  • CommonMark
3 / 35

Rmarkdown

Mix

  • Markdown

    • paragraph structure
    • comments
    • links
  • R code ("chunks")

  • Output of R code

4 / 35

Your first Rmarkdown file

Who has not been able to install Rmarkdown and Latex ?

5 / 35

Your first Rmarkdown file

Who has not been able to install Rmarkdown and Latex ?

5 / 35

Your first Rmarkdown file

6 / 35

Your first Rmarkdown file

Knit to HTML

Save to "xxx.Rmd"

7 / 35

Your first Rmarkdown file

8 / 35

Your first Rmarkdown file

Knit of pdf

9 / 35

Your first Rmarkdown file

10 / 35

Markdown syntax

Headings

# Heading - level 1
## Heading - level 2
11 / 35

Markdown syntax

Headings

# Heading - level 1
## Heading - level 2

Heading - level 1

Heading - level 2

11 / 35

Markdown syntax

Paragraphs

Paragraphs are separated
by a blank line.
Two spaces at the end of a line
produces a line break.
12 / 35

Markdown syntax

Paragraphs

Paragraphs are separated
by a blank line.
Two spaces at the end of a line
produces a line break.

Paragraphs are separated by a blank line.

Two spaces at the end of a line
produces a line break.

12 / 35

Markdown syntax

Formatting

_italic_, *italic*, **bold**, `monospace`.
13 / 35

Markdown syntax

Formatting

_italic_, *italic*, **bold**, `monospace`.

italic, italic, bold, monospace.

Do not mix straight and backward quotes

13 / 35

Markdown syntax

Bullet lists

Bullet list:
* apples
* oranges
* pears
* passe crassane (4 spaces to indent)
14 / 35

Markdown syntax

Bullet lists

Bullet list:
* apples
* oranges
* pears
* passe crassane (4 spaces to indent)
  • apples
  • oranges
  • pears
    • passe crassane
14 / 35

Markdown syntax

Numbered lists

Numbered list:
1. wash
1. rinse
1. repeat
15 / 35

Markdown syntax

Numbered lists

Numbered list:
1. wash
1. rinse
1. repeat
  1. wash
  2. rinse
  3. repeat
15 / 35

Markdown syntax

[Text of the link](URL of the link)
# Example
[Markdown syntax](https://www.markdownguide.org/basic-syntax/)
16 / 35

Markdown syntax

[Text of the link](URL of the link)
# Example
[Markdown syntax](https://www.markdownguide.org/basic-syntax/)

Markdown syntax

16 / 35

Markdown syntax

Images

![Image name](URL of the link - can also be a local file on your computer)
# Example
![](https://i.pinimg.com/originals/1c/b7/24/1cb724b545de717ed0430e9d46e78eea.jpg)
17 / 35

Markdown syntax

Images

![Image name](URL of the link - can also be a local file on your computer)
# Example
![](https://i.pinimg.com/originals/1c/b7/24/1cb724b545de717ed0430e9d46e78eea.jpg)

17 / 35

Markdown syntax

Tables

ID | First | Last
--- | --- | ---
1 | Jo | Biden
18 / 35

Markdown syntax

Tables

ID | First | Last
--- | --- | ---
1 | Jo | Biden
ID First Last
1 Jo Biden
18 / 35

Rmarkdown conversion

19 / 35

Rmarkdown conversion

  • knitr : R library

  • Pandoc : command line tool

    • Converts from md to pdf, html, docx...
# HTML
> pandoc test1.md -f markdown -t html -s -o test1.html
# pdf
> pandoc test1.md -s -o test1.pdf

19 / 35

The Rmarkdown file

File structure

20 / 35

The Rmarkdown file

Knit process

21 / 35

The Rmarkdown file

Output

22 / 35

R Chunks

Insert R chunk

23 / 35

R Chunks

Run R chunk

  • Two options

24 / 35

R Chunks

Option 1: Run R chunk inside Rmd file

  • Use when building and debugging an Rmd file

25 / 35

R Chunks

Option 2: Knit R chunk to HTML

  • Use for final production

26 / 35

R Chunks

Options for R chunks

27 / 35

R Chunks

Useful options


Options Default value Aim
echo TRUE Print code (= FALSE in a report for example)
eval TRUE Evaluate code (= FALSE if want to show code only)
warning TRUE Warning message (= FALSE to remove long warnings)
message TRUE Messages (= FALSE to remove long messages)
cache FALSE If TRUE only modified chunks will be evaluated
very useful for computing heavy codes
fig.height inches
fig.width inches
28 / 35

R Chunks

Set defaults options

  • Insert in a R chunk at the beginning of your Rmarkdown file
  • Option CACHE = true reduce compiling time
knitr::opts_chunk$set(fig.width=6, fig.height=6,
eval=TRUE,
cache=TRUE,
echo=TRUE,)
29 / 35

What can you do with Rmarkdown ?

Document your data analyses

  • If the data changes, you can re-run analysis in a matter of minutes
  • More and more journal request analyses scripts
30 / 35

What can you do with Rmarkdown ?

Presentation

31 / 35

What can you do with Rmarkdown ?

Posters

32 / 35

What can you do with Rmarkdown ?

Curriculum vitae

33 / 35

What can you do with Rmarkdown ?

Website

34 / 35

What did you learn ?




  • Rmarkdown: mix text, R chunk, R output
35 / 35

What did you learn ?




  • Rmarkdown: mix text, R chunk, R output
  • Compile to HTML or to PDF
35 / 35

What did you learn ?




  • Rmarkdown: mix text, R chunk, R output
  • Compile to HTML or to PDF
  • Can be used for many different purposes
35 / 35

What did you learn ?




  • Rmarkdown: mix text, R chunk, R output
  • Compile to HTML or to PDF
  • Can be used for many different purposes
  • Use to document your analysis process (for papers...)
35 / 35

Outline



  • What is Markown ?
  • Rmarkdown syntax
  • R chunks
  • Some applications
2 / 35
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