class: center, middle, inverse, title-slide # Introduction to {rmarkdown} ## R-ladies Barcelona ### Sarah Bonnin ### 2019-12-12 --- ## What is Markdown ? -- * **Lightweight markup language** -- * **Easy-to-read** and **easy-to-write** plain text format -- * Rendered as **HTML** document through [Blackfriday](https://gohugo.io/getting-started/configuration/) --- ## What is R Markdown ? -- * Advanced version of "plain" Markdown: **embeds** and **executes R code** -- * **Simple formatting syntax** for authoring **HTML, PDF, and MS Word** documents. -- * Easy to **report** and **share** analysis. -- * **Reproducibility** and **transparency**. -- * Produce **high quality documents**. -- <img src="https://bookdown.org/yihui/rmarkdown/images/hex-rmarkdown.png" width="30%" style="display: block; margin: auto;" /> --- ## Install and load {rmarkdown} package is **open-source** and can be installed and loaded as follows: ```r install.packages("rmarkdown") ``` ```r library(rmarkdown) ``` --- ## R Markdown Cheatsheet R Markdown [Cheatsheet](https://rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf) --- ## R Markdown (RMD) file * Plain text file -- * **.Rmd** extension -- * Create, write and run from **R Studio** --- ## How does it work? -- <img src="https://d33wubrfki0l68.cloudfront.net/61d189fd9cdf955058415d3e1b28dd60e1bd7c9b/b739c/lesson-images/rmarkdownflow.png" width="90%" style="display: block; margin: auto;" /> -- * **{rmarkdown}** feeds the input **.Rmd** file to **{knitr}** -- * **{knitr}** creates a new **.md** file containing the output -- * The **.md** file is then fed to **pandoc** that creates the final output --- ## R Markdown file and rendering Example of a **source file** and its **rendering**: <img src="images/RMD_knit.png" width="100%" style="display: block; margin: auto;" /> --- ## Create an .Rmd file in R Studio File -> New File -> R Markdown... <img src="images/RMD_create.png" width="70%" style="display: block; margin: auto;" /> --- ## Create an .Rmd file in R Studio Choose output format: <img src="images/RMD_choose_output.png" width="70%" style="display: block; margin: auto;" /> --- ## Template .Rmd file <img src="images/RMD_template.png" width="65%" style="display: block; margin: auto;" /> --- ## First knit! Knit the template **.Rmd** file to HTML! <img src="images/RMD_knit_template.png" width="35%" style="display: block; margin: auto;" /> --- ## First knit! <img src="images/RMD_knit_template_output.png" width="60%" style="display: block; margin: auto;" /> --- ## Blocks * Header (optional) in **YAML** format surrounded by **3 dashes** -- * R code chunks surrounded by **3 back ticks** -- * (Formatted) text --- ## Header The YAML header is surrounded by 3 dashes (**---**) -- ```r --- title: "TEST" author: "Sarah Bonnin" date: "12/8/2019" output: html_document --- ``` -- Add a table of content: -- ```r --- title: "TEST" author: "Sarah Bonnin" date: "12/8/2019" output: html_document: toc: true --- ``` --- ## Format text ```r *Italic* / _Italic_ **Bold** / __Bold__ ***Bold + Italic*** / ___Bold + Italic___ superscript^2^ ~~strikethrough~~ ``` -- *Italic* / _Italic_ **Bold** / __Bold__ ***Bold + Italic*** / ___Bold + Italic___ Superscript^2^ ~~Strikethrough~~ --- ## Format text ### Headers ````r # Header 1 ## Header 2 ### Header 3 #### Header 4 ```` -- # Header 1 ## Header 2 ### Header 3 #### Header 4 --- ## Format text ### Headers -- * Headers will be automatically organized in the **table of content** ! -- ````r # Section 1 ## Subsection 1.1 Some text ## Subsection 1.2 More text # Section 2 ## Subsection 2.1 Text from section 2.1 ## Subsection 2.2 Text from section 2.2 ```` --- ## Format text ### Headers <img src="images/RMD_TOC_output.png" width="35%" style="display: block; margin: auto;" /> --- ## Lists ### unordered ```` * first item * second item * first sub-item * second sub-item ```` -- * first item * second item * first sub-item * second sub-item --- ## Lists ### ordered ````r 1. first ordered item 2. second ordered item + first sub-item + second sub-item ```` -- 1. first ordered item 2. second ordered item + first sub-item + second sub-item --- ## Code blocks ### Options Customize the rendering with **options**: -- <img src="images/RMD_options.png" width="95%" style="display: block; margin: auto;" /> --- ## Code blocks ### Load packages in the background -- * **.Rmd** files are "independent": packages you will need in your code should be loaded **in a code block inside the document** ! -- * If you want to show **no output** and **no message** but still **execute the code**, use the option **include = FALSE** -- * Load the packages you will need to run the code of your analysis, **at the beginning of the .Rmd file**: -- ````r ```{r, include = FALSE} library(ggplot2) library(dplyr) library(kableExtra) ``` ```` --- ## Code blocks: examples The following code: ````r ```{r, eval=TRUE, echo=TRUE, message=TRUE} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: ```r ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ![](191212_rladies_rmarkdown_files/figure-html/unnamed-chunk-19-1.png)<!-- --> --- ## Code blocks: examples The following code: ````r ```{r, eval=TRUE, echo=TRUE, message=TRUE, fig.height=3} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: ```r ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ``` ## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. ``` ![](191212_rladies_rmarkdown_files/figure-html/unnamed-chunk-20-1.png)<!-- --> --- ## Code blocks: examples The following code: ````r ```{r, eval=TRUE, echo=TRUE, message=FALSE, fig.height=3} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: ```r ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ![](191212_rladies_rmarkdown_files/figure-html/unnamed-chunk-21-1.png)<!-- --> --- ## Code blocks: examples The following code: ````r ```{r, eval=TRUE, echo=FALSE, message=FALSE, fig.height=3} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: ![](191212_rladies_rmarkdown_files/figure-html/unnamed-chunk-22-1.png)<!-- --> --- ## Code blocks: examples The following code: ````r ```{r, eval=FALSE, echo=TRUE, message=FALSE, fig.height=3} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: ```r ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` --- ## Code blocks: examples The following code: ````r ```{r, eval=FALSE, echo=FALSE, message=FALSE, fig.height=3} ggplot(data=cars, aes(x=speed)) + geom_histogram() ``` ```` -- Renders as: -- NOTHING! No code, no output, no message... --- ## Code blocks: examples (tidy) The following code: ````r ```{r, eval=FALSE, echo=TRUE, message=FALSE, fig.height=3, tidy=TRUE} ggplot(data=cars,aes(x=speed))+geom_histogram() ``` ```` -- Renders as: ```r ggplot(data = cars, aes(x = speed)) + geom_histogram() ``` --- ## Code blocks Include options globally: ````r ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, tidy = TRUE, message = FALSE) ``` ```` --- ## Code blocks: Python and bash {rmarkdown} can also execute, for example, **bash** and **Python** blocks of code. -- ````bash ```{bash} ls * ``` ```` -- ```bash ls * ``` ``` ## 191212_rladies_rmarkdown.html ## 191212_rladies_rmarkdown.Rmd ## index.html ## README.md ## ## 191212_rladies_rmarkdown_files: ## figure-html ## ## images: ## RMD_choose_output.png ## RMD_create.png ## RMD_knit.png ## RMD_knit_template_output.png ## RMD_knit_template.png ## RMD_options.png ## RMD_template.png ## RMD_TOC_output.png ## ## libs: ## kePrint-0.0.1 ## remark-css-0.0.1 ``` --- ## Code blocks: Python and bash {rmarkdown} can also execute, for example, **bash** and **Python** blocks of code. -- ````python ```{python} x = 'Rladies Barcelona' print(x.split(' ')) ``` ```` -- ```python x = 'Rladies Barcelona' print(x.split(' ')) ``` ``` ## ['Rladies', 'Barcelona'] ``` --- ## Code blocks: knitr language engines {rmarkdown} can execute **many different languages!** -- See this [knitr language engines guide](https://bookdown.org/yihui/rmarkdown/language-engines.html) --- ## Insert images Recommended / easier to format way with **knitr::include_graphics**: -- ````r ```{r} knitr::include_graphics("https://rladies.org/wp-content/uploads/2016/12/R-LadiesGlobal.png") ``` ```` -- ```r knitr::include_graphics("https://rladies.org/wp-content/uploads/2016/12/R-LadiesGlobal.png") ``` ![](https://rladies.org/wp-content/uploads/2016/12/R-LadiesGlobal.png)<!-- --> --- ## Insert images Recommended / easier to format way with **knitr::include_graphics**: -- ````r ```{r, out.width="50%", echo=FALSE, fig.align="center"} knitr::include_graphics("https://rladies.org/wp-content/uploads/2016/12/R-LadiesGlobal.png") ``` ```` -- <img src="https://rladies.org/wp-content/uploads/2016/12/R-LadiesGlobal.png" width="50%" style="display: block; margin: auto;" /> --- ## Links You can **link** web pages: -- ````r Here is a [link]("https://rmarkdown.rstudio.com/") to {rmarkdown} page on [R Studio](https://rstudio.com/) website. ```` -- Here is a [link]("https://rmarkdown.rstudio.com/") to {rmarkdown} page on [R Studio](https://rstudio.com/) website. --- ## Create tables You can create tables: ````r Date | Workshop ----- | ----- 12/12/2019 | R Markdown 21/01/2020 | Forecasting ```` -- Date | Workshop ----- | ----- 12/12/2019 | R Markdown 21/01/2020 | Forecasting --- ## Show R objects as tables Format tables with **knitr::kable** and [**{kableExtra}**](http://haozhu233.github.io/kableExtra/awesome_table_in_html.html) package. ````r ```{r} knitr::kable(head(iris)) ``` ```` -- <table> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 5.1 </td> <td style="text-align:right;"> 3.5 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 3.0 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.7 </td> <td style="text-align:right;"> 3.2 </td> <td style="text-align:right;"> 1.3 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.6 </td> <td style="text-align:right;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.0 </td> <td style="text-align:right;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.4 </td> <td style="text-align:right;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables **Bold** columns ````r ```{r} knitr::kable(head(iris)) %>% column_spec(1:2, bold = TRUE) ``` ```` -- <table> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;font-weight: bold;"> 5.1 </td> <td style="text-align:right;font-weight: bold;"> 3.5 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 4.9 </td> <td style="text-align:right;font-weight: bold;"> 3.0 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 4.7 </td> <td style="text-align:right;font-weight: bold;"> 3.2 </td> <td style="text-align:right;"> 1.3 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 4.6 </td> <td style="text-align:right;font-weight: bold;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.0 </td> <td style="text-align:right;font-weight: bold;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.4 </td> <td style="text-align:right;font-weight: bold;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables **Colored** rows ````r ```{r} knitr::kable(head(iris)) %>% row_spec(1:3, color = "white", background = "red") ``` ```` -- <table> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;color: white !important;background-color: red !important;"> 5.1 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 3.5 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;"> 4.9 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 3.0 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;"> 4.7 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 3.2 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.3 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.6 </td> <td style="text-align:right;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.0 </td> <td style="text-align:right;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.4 </td> <td style="text-align:right;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables Change **font size** ````r ```{r} knitr::kable(head(iris)) %>% kable_styling(font_size = 10) ``` ```` -- <table class="table" style="font-size: 10px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 5.1 </td> <td style="text-align:right;"> 3.5 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 3.0 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.7 </td> <td style="text-align:right;"> 3.2 </td> <td style="text-align:right;"> 1.3 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.6 </td> <td style="text-align:right;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.0 </td> <td style="text-align:right;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.4 </td> <td style="text-align:right;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables All together ````r ```{r} knitr::kable(head(iris)) %>% kable_styling(font_size = 10) %>% row_spec(1:3, color = "white", background = "red") %>% column_spec(1:2, bold = TRUE) ``` ```` -- <table class="table" style="font-size: 10px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 5.1 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.5 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 4.9 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.0 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 4.7 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.2 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.3 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 4.6 </td> <td style="text-align:right;font-weight: bold;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.0 </td> <td style="text-align:right;font-weight: bold;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.4 </td> <td style="text-align:right;font-weight: bold;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables Add extra headers ````r ```{r} knitr::kable(head(iris)) %>% kable_styling(font_size = 10) %>% row_spec(1:3, color = "white", background = "red") %>% column_spec(1:2, bold = TRUE) %>% add_header_above(c("Sepal" = 2, "Petal" = 2, " " = 1)) ``` ```` -- <table class="table" style="font-size: 10px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;text-align: center; " colspan="2"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">Sepal</div></th> <th style="border-bottom:hidden; padding-bottom:0; padding-left:3px;padding-right:3px;text-align: center; " colspan="2"><div style="border-bottom: 1px solid #ddd; padding-bottom: 5px; ">Petal</div></th> <th style="border-bottom:hidden" colspan="1"></th> </tr> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:right;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 5.1 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.5 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 4.9 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.0 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.4 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 4.7 </td> <td style="text-align:right;color: white !important;background-color: red !important;font-weight: bold;"> 3.2 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 1.3 </td> <td style="text-align:right;color: white !important;background-color: red !important;"> 0.2 </td> <td style="text-align:left;color: white !important;background-color: red !important;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 4.6 </td> <td style="text-align:right;font-weight: bold;"> 3.1 </td> <td style="text-align:right;"> 1.5 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.0 </td> <td style="text-align:right;font-weight: bold;"> 3.6 </td> <td style="text-align:right;"> 1.4 </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;font-weight: bold;"> 5.4 </td> <td style="text-align:right;font-weight: bold;"> 3.9 </td> <td style="text-align:right;"> 1.7 </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Show R objects as tables Conditional formatting ````r ```{r} head(iris) %>% mutate( Petal.Length = cell_spec(Petal.Length, "html", color = ifelse(Petal.Length > 1.4, "red", "blue")) ) %>% kable(format="html", escape=FALSE) %>% kable_styling("striped", full_width = F, font_size = 10) ``` ```` -- <table class="table table-striped" style="font-size: 10px; width: auto !important; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;"> Sepal.Length </th> <th style="text-align:right;"> Sepal.Width </th> <th style="text-align:left;"> Petal.Length </th> <th style="text-align:right;"> Petal.Width </th> <th style="text-align:left;"> Species </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 5.1 </td> <td style="text-align:right;"> 3.5 </td> <td style="text-align:left;"> <span style=" color: blue !important;">1.4</span> </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.9 </td> <td style="text-align:right;"> 3.0 </td> <td style="text-align:left;"> <span style=" color: blue !important;">1.4</span> </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.7 </td> <td style="text-align:right;"> 3.2 </td> <td style="text-align:left;"> <span style=" color: blue !important;">1.3</span> </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 4.6 </td> <td style="text-align:right;"> 3.1 </td> <td style="text-align:left;"> <span style=" color: red !important;">1.5</span> </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.0 </td> <td style="text-align:right;"> 3.6 </td> <td style="text-align:left;"> <span style=" color: blue !important;">1.4</span> </td> <td style="text-align:right;"> 0.2 </td> <td style="text-align:left;"> setosa </td> </tr> <tr> <td style="text-align:right;"> 5.4 </td> <td style="text-align:right;"> 3.9 </td> <td style="text-align:left;"> <span style=" color: red !important;">1.7</span> </td> <td style="text-align:right;"> 0.4 </td> <td style="text-align:left;"> setosa </td> </tr> </tbody> </table> --- ## Hands on ! Modify the **.Rmd template**: -- * Modify global options so blocks of code are always **tidy**. -- * **plot(pressure)**: show code but hide plot. -- * Format **summary(cars)** with **kable** and **{kableExtra}**: highlight in yellow the "Median" row. -- * Add the **table of content** (toc) option in the YAML header -- * Add any modification you want (text, formatting, ...) -- * **knit** ! --- class: inverse, center, middle # THANK YOU ! -- *slides created with the [xaringan package](https://github.com/yihui/xaringan)* -- Follow us on Twitter ! @RLadiesBCN <img src="https://media.giphy.com/media/SMKiEh9WDO6ze/giphy.gif" width="55%" style="display: block; margin: auto;" />