Science Communication with R: Style, R Markdown & Xaringan

Original Xaringan slides by Antoine are available here

Code presentation


  • Use <- for assignation and = for arguments

  • Leave a space

  • before ( (except when it’s part of a function call)

  • after ) or ,

  • around =, <-, +, *, ==, etc

x <- rnorm(n = 100000)^2 + rnorm(n = 100000)^2
hist(x[x < 10], col = "grey", xlab = "", ylab = "Count", 
     main = "Chi-squared empirical distribution")

Functions names

  • When possible, use verbs for function names: add_row() or instead of row_adder()
  • A function should do one thing well (it should be called either because it has side-effects or because it returns a value; not both)
  • Don’t hesitate to split your long function in smaller ones
  • Only use return() for early returns
  • Comment your functions (use the Ctrl + Maj + C shortcut)


  • For long pipes, use one verb per line
  • Don’t ommit the parenthesis
# Good
summary_SL <-
  iris %>% 
  drop_na() %>% 
  group_by(Species) %>% 
  summarise(Mean_SL = mean(Sepal.Length))

# Bad
summary_SL <- iris %>% drop_na %>% group_by(Species) %>% 

iris %>% 
  drop_na %>% 
  group_by(Species) %>% 
  summarise(Mean_SL=mean(Sepal.Length)) -> summary_SL

Useful help

  • The Ctrl + Maj + A shortcut rearranges your code

  • {styler} package

ugly_code <- "a<-function( x){1+1}           "
## a <- function(x) {
##   1 + 1
## }

R Markdown

R Markdown?

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

Relationship between R Markdown, Knitr, Pandoc, and Bookdown

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

  • rmarkdown is a a library which proceses and converts .Rmd files into a number of different formats, including HTML or .pdf. The core function is rmarkdown::render().


Create a new .Rmd

Alt Alt

New .Rmd



Compile .Rmd

Use the Knit button to produce a HTML file



Shortcut: Ctrl + Maj + K

Markdown syntax

Titles and text fonts

# Big title

## Title

### Small title

_Italic_, *italic*, __bold__, **bold**, and `monospace`

Big title


Small title

Italic, italic, bold, bold, and monospace

Lists and more

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 insert quotes, $\LaTeX$ expressions and horizontal rule:

You can insert quotes, \(\LaTeX\) expressions and horizontal rule:


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

\[ \frac{1}{n} \sum_{i=1}^n X_i \]


Basic chunk

x <- 4


x <- 4
## [1] 4


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

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


## [1] 5


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

```{r chunkeval, eval=FALSE}
x <- 5


x <- 10
## [1] 5

Fig options

```{r chunkfig, fig.align="center", fig.height=4, fig.width=8}
plot(iris$Sepal.Length, iris$Sepal.Width)
plot(iris$Sepal.Length, iris$Sepal.Width)

Finding the good picture size is a trial and error process…

Better way to include picture

```{r betterway, fig.align="center", out.width="70%", echo=FALSE}

It allows you to use the chunk options for image size

Name your chunks!

  • In RStudio you can navigate named chunks fairly easily

Pets or livestock? Naming your RMarkdown chunks

  • The goal of named chunk is clear (or it should be)

  • Easier to debug

  • Caching is more efficient with named chunks (otherwise it uses chunk positions)



Xaringan is a package to create slides with R Markdown

It is developped by Yihui Xie @yihui



How it works?

The syntax is almost the same as R Markdown

Slides are separated by ---

You can build incremental slides with --

# How it works?

The syntax is almost the same as R Markdown


Slides are separated by `---`


You can build incremental slides with `--` 


You can change slide configuration (for titles) with class

class: middle, center, inverse

To skip a lign, use the <br> HTML tag

Use .footnote[Text] to add a footnote and .center[Text] to center text

Include an interactive map with leaflet

leaflet() %>% addTiles() %>% setView(2.34822, 48.83976, zoom = 17)