June 17th, 2019

Literate programming

80’s idea from D. Knuth:

write code for humans, not for computer

Dynamic reporting

One document to rule them all with both text & code
=> export to different formats

source: Rmarkdown cheatsheet

Reproducible research

In R: .rmd (“Rmarkdown”)

  • markdown: lightweight structured text

  • Rmarkdown = R + markdown

  • Rmarkdown + pandoc
    => output in many formats (.docx, .pdf, .html, etc…)

source: Rmarkdown cheatsheet

These materials have been prepared in Rmarkdown

.Rmd structure

  1. YAML Header
---
title: 'BADAS: Part II'
subtitle: "Dynamic & Reproducible reporting with `Rmarkdown`"
author: "Boris Hejblum & Loïc Ferrer"
date: "June 17, 2019"
output: 
  ioslides_presentation:
    number_sections: no
---
  • Text
    Narration

  • Code Chunks

```{r}
```

Code chunks

Basic chunk options

  • results (default is 'markup')

    • 'asis' - passthrough results
    • 'hide' - do not display results
    • 'hold' - put all results below all code
  • eval - Run code in chunk (default is TRUE)

  • echo - Display code in output document (default is TRUE)

  • message - display code messages in document (default is TRUE)

  • error - Display error messages in output document (TRUE) or stop render when errors occur (FALSE)
    (default is FALSE)

Code display options

  • tidy - tidy code for display (default is FALSE)
  • warning - display code warnings in document (default = TRUE)
  • collapse - collapse all output into single block (default = FALSE)
  • highlight - highlight source code (default = TRUE)
  • include - include chunk in doc after running (default = TRUE)
  • comment - prefix for each line of results (default = ‘##’)

Plots & figures

  • fig.align - figure postion: 'left', 'right', or 'center'
  • fig.cap - figure caption as character string (default is NULL)
  • fig.height, fig.width - Dimensions of plots in inches
  • out.width - only in pdf: size of the figure

cache

Long running code chunks

  • cache - cache results for future knits (default is FALSE)
  • cache.path - directory to save cached results in (default is "cache/")
  • dependson - chunk dependencies for caching (default is NULL)

Additional features

Multiple files

  • child - file(s) to knit and then include (default is NULL)

not just R (python, SAS…)

  • engine - code language used in chunk (default = ‘R’)

Text

Markdown syntax

  • *italic*italic
  • **bold**bold
  • ~~no~~strikethrough
  • `code` — verbatim code
  • ^2^ — superscript2
  • ~2~ — subscript2
  • $\alpha = \beta^2$ — LaTeX equations: \(\alpha = \beta^2\)
  • End a line with two spaces to start a new paragraph

Headings & cross-references

Headings

# Header1 {#anchor}
## Header 2

Jump to [Header 1](#anchor)

Footnotes

A footnote [^1]
[^1]: Here is the footnote.

Include images

![Caption](pathtoimage.png)

Lists and Bullets in markdown

 - Bullet 1
 - Bullet 2
  • Bullet 1
  • Bullet 2
1. item 1
2. item 2
  1. item 1
  2. item 2

Tables

| Right | Left | Default | Center |
|------:|:-----|---------|:------:|
| 12 | 12 | 12 | 12|
| 123 | 123 | 123 | 123 |
| 1 | 1 | 1 | 1 |

Right Left Default Center
12 12 12 12
123 123 123 123
1 1 1 1

Bibliography

---
bibliography: refs.bib 
csl: style.csl
---

Smith cited [@smith04].
Smith cited without author [-@smith04].
@smith04 cited in line.

Additional resources

Rmd practical

  1. create a new .Rmd file
  2. play along with the various options described here
  3. try various output formats
  4. turn your answers to the first practical into a .Rmd