class: center, middle, inverse, title-slide # Science Communication with R ## Style, R Markdown & Xaringan ### Antoine Bichat ### State of the R - AgroParisTech
February 23, 2018 --- class: inverse, center, middle # Code presentation --- # Syntax * Use `<-` for assignation and `=` for arguments * Leave a space * before `(` (except when it's part of a function call) * after `)` or `,` * around `=`, `<-`, `+`, `*`, `==`, etc ```r x <- rnorm(n = 100000)^2 + rnorm(n = 100000)^2 hist(x[x < 10], col = "grey", xlab = "", ylab = "Count", main = "Chi-squared empirical distribution") ``` <img src="index_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> --- # 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`) --- # Pipes * For long pipes, use one verb per line * Don't ommit the parenthesis ```r # Good summary_SL <- iris %>% drop_na() %>% group_by(Species) %>% summarise(Mean_SL = mean(Sepal.Length)) # Bad summary_SL <- iris %>% drop_na %>% group_by(Species) %>% summarise(Mean_SL=mean(Sepal.Length)) 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 ```r ugly_code <- "a<-function( x){1+1} " styler::style_text(ugly_code) ``` ``` ## a <- function(x) { ## 1 + 1 ## } ``` --- class: inverse, center, middle # R Markdown <img src="https://www.rstudio.com/wp-content/uploads/2014/04/rmarkdown-200x232.png" width="200" height="232"> --- #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`. .footnote[[
Relationship between R Markdown, Knitr, Pandoc, and Bookdown](https://stackoverflow.com/questions/40563479)] -- * 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()`. ```r install.packages("rmarkdown") ``` --- # Create a new `.Rmd` .pull-left[ .center[ <img src="CreateRmd1.png" width="400"> ] ] -- .pull-right[ <div style = "margin-top: 80px"></div> .center[ <img src="CreateRmd2.png" width="400"> ] ] --- # New `.Rmd` .center[ <img src="CreateRmd3.png" width="700"> ] --- # Compile `.Rmd` Use the `Knit` button to produce a HTML file .center[ <img src="CreateRmd4.png" width="300"> ] Shortcut: Ctrl + Maj + K --- class: center, middle # Markdown syntax --- # Titles and text fonts ``` # Big title ## Title ### Small title _Italic_, *italic*, __bold__, **bold**, and `monospace` ``` # Big title ## Title ### Small title _Italic_, *italic*, __bold__, **bold**, and `monospace` --- # Lists and more <div style = "margin-top: -30px"></div> .pull-left[ ``` 1. This is 2. an ordered 3. list ``` 1. This is 2. an ordered 3. list ] -- .pull-right[ ``` * This is * a bullet list * with indent ``` * This is * a bullet list * with indent ] <br> -- ``` > 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 $$ --- # Insert links and pictures ``` [My personal page](https://abichat.github.io/) ``` [My personal page](https://abichat.github.io/) -- ``` ![](https://slides.yihui.name/gif/impossible-parking.gif) ``` ![](https://slides.yihui.name/gif/impossible-parking.gif) -- It also works directly with HTML syntax: ``` <a href="https://abichat.github.io/">My personal page</a> <img src="https://slides.yihui.name/gif/impossible-parking.gif"> ``` --- class: middle, center # Chunks --- # Basic chunk ```` ```{r} x <- 4 x ``` ```` -- Output: ```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 chunkecho, echo=FALSE} x <- 5 x ``` ```` -- Output: ``` ## [1] 5 ``` --- # Eval To display R code without evaluating it, you specify the `eval=FALSE` chunk option ```` ```{r chunkeval, eval=FALSE} x <- 5 ``` ```` -- Output: ```r x <- 10 ``` -- ```r x ``` ``` ## [1] 5 ``` --- # Fig options ```` ```{r chunkfig, fig.align="center", fig.height=4, fig.width=8} plot(iris$Sepal.Length, iris$Sepal.Width) ``` ```` -- ```r plot(iris$Sepal.Length, iris$Sepal.Width) ``` <img src="index_files/figure-html/chunkfig-1.png" style="display: block; margin: auto;" /> -- 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} knitr::include_graphics( "https://slides.yihui.name/gif/impossible-parking.gif") ``` ```` -- <img src="https://slides.yihui.name/gif/impossible-parking.gif" width="70%" style="display: block; margin: auto;" /> -- It allows you to use the chunk options for image size --- # Name your chunks! * In RStudio you can navigate named chunks fairly easily .footnote[[
Pets or livestock? Naming your RMarkdown chunks](http://www.masalmon.eu/2017/08/08/chunkpets/)] -- * 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) --- class: inverse, center, middle # Xaringan <img src="https://camo.githubusercontent.com/f91f64981633838bdedfee54df8595534c3cd9ba/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f622f62652f53686172696e67616e5f747269706c652e737667" width="232" height="232"> --- # Xaringan? Xaringan is a package to create slides with R Markdown -- It is developped by Yihui Xie
<a href="https://github.com/yihui">@yihui</a> ```r devtools::install_github('yihui/xaringan') ``` -- .center[ <img src="CreateRmd5.png" width="400"> ] --- # How it works? The syntax is almost the same as R Markdown -- Slides are separated by `---` -- You can build incremental slides with `--` -- <br> ``` --- # How it works? The syntax is almost the same as R Markdown -- Slides are separated by `---` -- You can build incremental slides with `--` ``` --- # Tips You can change slide configuration (for titles) with `class` ``` --- class: middle, center, inverse ``` <br> -- To skip a lign, use the `<br>` HTML tag -- <br> Use `.footnote[Text]` to add a footnote and `.center[Text]` to center text -- <br> .center[I'm centered] .footnote[I'm a footnote] --- # Include an interactive map with leaflet ```r library(leaflet) leaflet() %>% addTiles() %>% setView(2.34822, 48.83976, zoom = 17) ```
--- class: middle, center, inverse # References --- ## Style * The tidyverse style guide http://style.tidyverse.org ## R Markdown * Official site https://rmarkdown.rstudio.com * Cheat Sheet https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf ## Xaringan * Documentation https://slides.yihui.name/xaringan --- class: center, middle, inverse # Thanks for your attention! ####
<a href="https://github.com/abichat" target="_blank">@abichat</a> <div style = "margin-top: -20px"></div> ####
<a href="https://abichat.github.io" target="_blank">abichat.github.io</a> <div style = "margin-top: -20px"></div> ####
<a href="mailto:antoine.bichat@mines-nancy.org?subject=Science%20Communication%20with%20R">antoine.bichat@mines-nancy.org</a> .footnote[Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan).]