class: center, middle, inverse, title-slide # R Markdown & LaTeX ## Lecture 3 ###
Louis SIRUGUE ### M1 APE - Fall 2022 --- ### Quick reminder <p style = "margin-bottom:2cm;"> <center><h4> The 3 core components of the ggplot() function </h4></center> -- <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption></caption> <thead> <tr> <th style="text-align:left;"> Component </th> <th style="text-align:center;"> Contribution </th> <th style="text-align:center;"> Implementation </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Data </td> <td style="text-align:center;"> Underlying values </td> <td style="text-align:center;"> ggplot(data, | data %>% ggplot(., </td> </tr> <tr> <td style="text-align:left;"> Mapping </td> <td style="text-align:center;"> Axis assignment </td> <td style="text-align:center;"> aes(x = V1, y = V2, ...)) </td> </tr> <tr> <td style="text-align:left;"> Geometry </td> <td style="text-align:center;"> Type of plot </td> <td style="text-align:center;"> + geom_point() + geom_line() + ... </td> </tr> </tbody> </table> <p style = "margin-bottom:2cm;"> -- * Any **other element** should be added with a **`+` sign** -- ```r ggplot(data, aes(x = V1, y = V2)) + geom_point() + geom_line() + anything_else() ``` --- ### Quick reminder .pull-left[ <p style = "margin-bottom:1.75cm;"> <center><h4> Main customization tools </h4></center> <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption></caption> <thead> <tr> <th style="text-align:left;"> Item to customize </th> <th style="text-align:left;"> Main functions </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Axes </td> <td style="text-align:left;"> scale_[x/y]_[continuous/discrete] </td> </tr> <tr> <td style="text-align:left;"> Baseline theme </td> <td style="text-align:left;"> theme_[void/minimal/.../dark]() </td> </tr> <tr> <td style="text-align:left;"> Annotations </td> <td style="text-align:left;"> geom_[[h/v]line/text](), annotate() </td> </tr> <tr> <td style="text-align:left;"> Theme </td> <td style="text-align:left;"> theme(axis.[line/ticks].[x/y] = ..., </td> </tr> </tbody> </table> ] -- .pull-right[ <center><h4> Main types of geometry </h4></center> <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption></caption> <thead> <tr> <th style="text-align:left;"> Geometry </th> <th style="text-align:center;"> Function </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Bar plot </td> <td style="text-align:center;"> geom_bar() </td> </tr> <tr> <td style="text-align:left;"> Histogram </td> <td style="text-align:center;"> geom_histogram() </td> </tr> <tr> <td style="text-align:left;"> Area </td> <td style="text-align:center;"> geom_area() </td> </tr> <tr> <td style="text-align:left;"> Line </td> <td style="text-align:center;"> geom_line() </td> </tr> <tr> <td style="text-align:left;"> Density </td> <td style="text-align:center;"> geom_density() </td> </tr> <tr> <td style="text-align:left;"> Boxplot </td> <td style="text-align:center;"> geom_boxplot() </td> </tr> <tr> <td style="text-align:left;"> Violin </td> <td style="text-align:center;"> geom_violin() </td> </tr> <tr> <td style="text-align:left;"> Scatter plot </td> <td style="text-align:center;"> geom_point() </td> </tr> </tbody> </table> ] --- ### Quick reminder .pull-left[ <center><h4> Main types of aesthetics </h4></center> <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption></caption> <thead> <tr> <th style="text-align:left;"> Argument </th> <th style="text-align:left;"> Meaning </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> alpha </td> <td style="text-align:left;"> opacity from 0 to 1 </td> </tr> <tr> <td style="text-align:left;"> color </td> <td style="text-align:left;"> color of the geometry </td> </tr> <tr> <td style="text-align:left;"> fill </td> <td style="text-align:left;"> fill color of the geometry </td> </tr> <tr> <td style="text-align:left;"> size </td> <td style="text-align:left;"> size of the geometry </td> </tr> <tr> <td style="text-align:left;"> shape </td> <td style="text-align:left;"> shape for geometries like points </td> </tr> <tr> <td style="text-align:left;"> linetype </td> <td style="text-align:left;"> solid, dashed, dotted, etc. </td> </tr> </tbody> </table> ] -- .pull-right[ <p style = "margin-bottom:3.25cm;"></p> <ul> <li>If specified <b>in the geometry</b></li> <ul> <li>It will apply uniformly to <b>all the geometry</b></li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul> <li>If assigned to a variable <b>in aes</b></li> <ul> <li>It will <b>vary with the variable</b> according to a scale documented in legend</li> </ul> </ul> ] <br> -- ```r ggplot(data, aes(x = V1, y = V2, size = V3)) + geom_point(color = "steelblue", alpha = .6) ``` --- class: inverse, hide-logo ### Warm up practice <ul> <li>Today we're going to use the <a href = "https://www.insee.fr/fr/statistiques/2540004?sommaire=4767262"><b><i>"Fichier des prénoms"</i></b></a></li> <ul> <li>This is where the INSEE reports the <b>birth count</b> associated with <b>each first name in France</b></li> <li>It is <b>virtually exhaustive from 1946</b>, when the INSEE was founded</li> </ul> </ul> <p style = "margin-bottom:1.25cm;"></p> -- ```r names <- read.csv("C:/User/Documents/fichier_prenoms.csv", sep = ";", encoding = "UTF-8") str(names) ``` ``` ## 'data.frame': 686538 obs. of 4 variables: ## $ sexe : int 1 1 1 1 1 1 1 1 1 1 ... ## $ preusuel: chr "_PRENOMS_RARES" "_PRENOMS_RARES" "_PRENOMS_RARES" "_PRENOMS_RARES" ... ## $ annais : chr "1900" "1901" "1902" "1903" ... ## $ nombre : int 1249 1342 1330 1286 1430 1472 1451 1514 1509 1526 ... ``` -- <p style = "margin-bottom:1.25cm;"></p> * **`sexe`:** `1` for Male and `2` for Female * **`preusuel`:** first name *(`_PRENOMS_RARES` gathers rare first names for anonymity considerations)* * **`annais`:** birth year *(`XXXX` groups unknown birth years)* * **`nombre`:** number of newborns for the corresponding sex/name/year --- class: inverse, hide-logo ### Warm up practice <p style = "margin-bottom:2cm;"></p> #### 1) Recode the `sexe` variable with `Male` and `Female` instead of `1` and `2` <p style = "margin-bottom:1.25cm;"></p> -- #### 2) Filter out observations for which `annais` is `XXXX` and convert `annais` to numeric <p style = "margin-bottom:1.25cm;"></p> -- #### 3) Summarise your data into the total number of births per year <p style = "margin-bottom:1.25cm;"></p> -- #### 4) Plot the evolution of the number of births over time using a line geometry <p style = "margin-bottom:2.5cm;"></p> -- <center><h3><i>You've got 10 minutes!</i></h3></center>
−
+
10
:
00
--- class: inverse, hide-logo ### Solution #### Load the necessary packages ```r library(dplyr) library(ggplot2) ``` -- <p style = "margin-bottom:1.25cm;"></p> #### 1) Recode the `sexe` variable with `Male` and `Female` instead of `1` and `2` -- ```r names %>% * mutate(sexe = ifelse(sexe == 1, "Male", "Female")) ``` -- <p style = "margin-bottom:1.25cm;"></p> #### 2) Filter out observations for which `annais` is `XXXX` and convert `annais` to numeric -- ```r names %>% mutate(sexe = ifelse(sexe == 1, "Male", "Female")) %>% * filter(annais != "XXXX") %>% * mutate(annais = as.numeric(annais)) ``` --- class: inverse, hide-logo ### Solution #### 3) Summarise your data into the total number of births per year -- ```r names %>% mutate(sexe = ifelse(sexe == 1, "Male", "Female")) %>% filter(annais != "XXXX") %>% mutate(annais = as.numeric(annais)) %>% * group_by(annais) %>% * summarise(n = sum(nombre)) ``` -- ``` ## # A tibble: 8 x 2 ## annais n ## <dbl> <int> ## 1 1900 415040 ## 2 1901 453456 ## 3 1902 465791 ## 4 1903 468810 ## 5 1904 478962 ## 6 1905 489697 ## 7 1906 501745 ## 8 1907 501025 ``` --- class: inverse, hide-logo ### Solution #### 4) Plot the evolution of the number of births over time using a line geometry -- ```r names %>% mutate(sexe = ifelse(sexe == 1, "Male", "Female")) %>% filter(annais != "XXXX") %>% mutate(annais = as.numeric(annais)) %>% group_by(annais) %>% summarise(n = sum(nombre)) %>% * ggplot(aes(x = annais, y = n)) + geom_line() ``` -- <img src="slides_files/figure-html/unnamed-chunk-16-1.png" width="55%" style="display: block; margin: auto;" /> --- <h3>Today we learn how to make reports with R Markdown!</h3> -- <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] .pull-right[ <ul style = "margin-left:-1cm;list-style: none"> <li><b>3. LaTeX for equations</b></li> <ul style = "list-style: none"> <li>3.1. What is LaTeX?</li> <li>3.2. LaTeX syntax</li> <li>3.3. Large equations</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:-1cm;list-style: none"><li><b>4. Wrap up!</b></li></ul> ] --- <h3>Today we learn how to make reports with R Markdown!</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> ] --- ### 1. Basic principles #### 1.1. What is R Markdown? <ul> <li><b>R Markdown</b> is a type of document in which you can both <b>write/run R code</b> and <b>edit text</b></li> </ul> <p style = "margin-bottom:1cm;"> -- <ul> <li>Here are some examples of R Markdown reports <i>(from my other course)</i></li> <ul> <li><a href="https://louissirugue.github.io/metrics_on_R/2021_homework1/correction.html">Last year homework</a></li> <li><a href="https://louissirugue.github.io/metrics_on_R/project/example">Example of research project</a></li> <li><a href="https://louissirugue.github.io/metrics_on_R/2021_covariance_vs_correlation_with_code.html">Supplementary material</a></li> <li><a href="https://louissirugue.github.io/metrics_on_R/home.html">Course webpage and material</a></li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- <ul> <li>It is structured around <b>3 types of content</b>:</li> <ul> <li><b>Code chunks</b> to run and render the output</li> <li><b>Editable text</b> to display</li> <li><b>YAML metadata</b> for the R Markdown build process</li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- <center><h4><i>➜ Let's go through them by creating our first R Markdown!</i></h4></center> --- ### 1. Basic principles #### 1.1. What is R Markdown? <center>➜ Click on <b>File > New File > Rmarkdown</b></center> <p style = "margin-bottom:-.5cm;"> -- .pull-left[ <img src = "new_markdown.png" width = "450"/> ] -- .pull-right[ <p style = "margin-bottom:2.5cm;"> <ol> <li>Fill out the information and select <b>HTML</b></li> <li style = "margin-top:7cm;">Click on <b>OK</b></li> </ol> ] --- ### 1. Basic principles #### 1.1. What is R Markdown? * It creates a **template** containing the **3 types of content**: -- .left-column[ <p style = "margin-bottom:1.25cm;"> <b> YAML header ➜</b> <p style = "margin-bottom:.75cm;"> <b> Code chunks ➜</b> <p style = "margin-bottom:1.25cm;"> <b> Text ➜</b> <p style = "margin-bottom:1.75cm;"> <center><i>Let's go through them one by one!</i></center> ] .right-column[ <center><img src = "template.png" width = "650"/></center> ] --- ### 1. Basic principles #### 1.2. YAML header * The **YAML header** contains general information related to the **file configuration**: <p style = "margin-bottom:-.25cm;"> -- <ul> <ul> <li>Title/subtitle (in quotes)</li> <li>Author (in quotes)</li> <li>Date (in quotes)</li> <li>Output type (html_document/pdf_document)</li> <li>...</li> </ul> </ul> -- <p style = "margin-bottom:1cm;"> * It should be specified at the **very beginning** of the document and surrounded by **three dashes** like so: ````markdown --- title: "My First Markdown Report" author: "Louis Sirugue" date: "24/09/2021" output: html_document --- ```` --- ### 1. Basic principles #### 1.3. Code chunks * **Code chunks are blocks of R code** that can be run when working on and rendering the .Rmd file -- <p style = "margin-bottom:1.5cm;"> * You can insert a code chunk using `Ctrl + Alt + i` or by typing the **backticks chunk delimiters** as follows ````markdown ```{r} 1+1 ``` ```` <p style = "margin-bottom:.5cm;"> -- <p style = "margin-bottom:1cm;"> <ul> <li>When <b>rendering</b> the document, R will <b>execute</b> the code</li> <ul> <li>Both the <b>code</b> and the <b>output</b> will appear in the document like so:</li> </ul> </ul> ```r 1+1 ``` ``` ## [1] 2 ``` --- ### 1. Basic principles #### 1.3. Code chunks * The **content** to be **displayed** from the code chunk can be specified in **chunk options** * For instance, to display only the output and not the code chunk, you can set `echo` to `FALSE` -- ````markdown ```{r, echo = F} 1+1 ``` ```` -- <p style = "margin-bottom:1cm;"> * And the output will only be ``` ## [1] 2 ``` -- <p style = "margin-bottom:1cm;"> * Instead of ```r 1+1 ``` ``` ## [1] 2 ``` --- ### 1. Basic principles #### 1.3. Code chunks <center><h4> Chunk options to know </h4></center> <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption></caption> <thead> <tr> <th style="text-align:left;text-align: center;"> Option </th> <th style="text-align:center;text-align: center;"> Default </th> <th style="text-align:left;text-align: center;"> Effect </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> eval </td> <td style="text-align:center;"> TRUE </td> <td style="text-align:left;"> Whether to evaluate the code and include its results </td> </tr> <tr> <td style="text-align:left;"> echo </td> <td style="text-align:center;"> TRUE </td> <td style="text-align:left;"> Whether to display code along with its results </td> </tr> <tr> <td style="text-align:left;"> warning </td> <td style="text-align:center;"> TRUE </td> <td style="text-align:left;"> Whether to display warnings </td> </tr> <tr> <td style="text-align:left;"> error </td> <td style="text-align:center;"> TRUE </td> <td style="text-align:left;"> Whether to display errors </td> </tr> <tr> <td style="text-align:left;"> message </td> <td style="text-align:center;"> TRUE </td> <td style="text-align:left;"> Whether to display messages </td> </tr> <tr> <td style="text-align:left;"> results </td> <td style="text-align:center;"> 'markup' </td> <td style="text-align:left;"> 'hide' to hide the output </td> </tr> <tr> <td style="text-align:left;"> fig.width </td> <td style="text-align:center;"> 7 </td> <td style="text-align:left;"> Width in inches for plots created in chunk </td> </tr> <tr> <td style="text-align:left;"> fig.height </td> <td style="text-align:center;"> 7 </td> <td style="text-align:left;"> Height in inches for plots created in chunk </td> </tr> </tbody> </table> --- ### 1. Basic principles #### 1.4. Text formatting <ul> <li>R Markdown is not only about rendering code but also about <b>writing</b> actual <b>text</b></li> <ul> <li>You can write <b>paragraphs</b> as you would normally do on a typical report</li> <li>And R Markdown provides convenient ways to <b>format</b> your text</li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- <ul> <li>Basic formatting includes:</li> <ul> <li>Italics</li> <li>Bold</li> <li>Hyperlinks</li> <li>Headers</li> <li>Block quotes</li> <li>Un/ordered lists</li> <li>...</li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- <ul> <li>Unlike most text editing software, in R Markdown <b>text formatting</b> isn't about clicking on dedicated buttons</li> <ul> <li>It <b>relies on symbols</b> that should be written along with the text</li> </ul> </ul> --- ### 1. Basic principles #### 1.4. Text formatting <p style = "margin-bottom:-1cm;"> .pull-left[ <center> <h4> Syntax </h4> </center> `Plain text ⠀` `End a line with two spaces for line break` `*italics*` `**bold**` `# Header 1` `## Header 2` ... `###### Header 6` `[link](https://www.rstudio.com)` ] .pull-right[ <center> <h4> Output </h4> </center> Plain text End a line with two spaces for line break *italics* **bold** <p style = "margin-bottom:-1cm;"> # Header 1 <p style = "margin-bottom:-1cm;"> ## Header 2 <p style = "margin-bottom:-1cm;"> ... <p style = "margin-bottom:-.5cm;"> ###### Header 6 <p style = "margin-bottom:-.5cm;"> [link](https://www.rstudio.com) ] --- ### 1. Basic principles #### 1.4. Text formatting <p style = "margin-bottom:-1cm;"> .pull-left[ <center> <h4> Syntax </h4> </center> `> block quote` Horizontal rule: `***` `* unordered list` `* item 2` `+ sub-item 1` `+ sub-item 2` `1. ordered list` `2. item 2` `+ sub-item 1` `+ sub-item 2` ] .pull-right[ <center> <h4> Output </h4> </center> > block quote Horizontal rule: <p style = "margin-bottom:.75cm;"> *** <p style = "margin-bottom:.75cm;"> * unordered list * item 2 + sub-item 1 + sub-item 2 1. ordered list 2. item 2 + sub-item 1 + sub-item 2 ] --- ### 1. Basic principles #### 1.5. Run and knit your code <ul> <li>To <b>execute</b> the content of a <b>code</b> chunk in R Markdown</li> <ul> <li>Click on the <b>green play button</b> at the top right of the chunk <img src = "current_chunk.png" width = "20"/></li> </ul> </ul> -- .pull-left[ <ul> <li>You can also:</li> <ul> <li><b>Run all chunks above</b> the current chunk <img src = "all_chunks_above.png" width = "20"/></li> <li><b>Run all chunks</b> from the Run drop down menu at the top right (or Ctrl+Alt+R)</li> </ul> </ul> <center><img src = "run_all.png" width = "185"/></center> ] -- .pull-left[ <ul> <li>To choose where the <b>output</b> must be <b>displayed</b>, click on the <i>"Options"</i> button</li> <ul> <li><b>Chunk output inline:</b> output displayed right</li> </ul> </ul> <p style = "margin-left:7.72cm; margin-top:-.55cm;">below the chunk in the</p> <p style = "margin-left:7.72cm; margin-top:-.55cm; margin-bottom:-.55cm;"><b>source panel</b></p> <ul><ul><li><b>Chunk output in console:</b> output displayed</li></ul></ul> <p style = "margin-left:8.88cm; margin-top:-.55cm;">in <b>console panel</b></p> <center><img src = "inline.png" width = "200"/></center> ] --- ### 1. Basic principles #### 1.5. Run and knit your code * To **render** an R Markdown file, click on the **knit button** <img src = "knit.png" width = "35"/> (`ctrl + shift + k`) -- <center><img src = "comparison_1.png" width = "900"/></center> --- ### 1. Basic principles #### 1.5. Run and knit your code * To **render** an R Markdown file, click on the **knit button** <img src = "knit.png" width = "35"/> (`ctrl + shift + k`) <center><img src = "comparison_2.png" width = "900"/></center> --- <h3>Overview</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles ✔</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] .pull-right[ <ul style = "margin-left:-1cm;list-style: none"> <li><b>3. LaTeX for equations</b></li> <ul style = "list-style: none"> <li>3.1. What is LaTeX?</li> <li>3.2. LaTeX syntax</li> <li>3.3. Large equations</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:-1cm;list-style: none"><li><b>4. Wrap up!</b></li></ul> ] --- <h3>Overview</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles ✔</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] --- ### 2. Useful features #### 2.1. Inline code * A big advantage of R Markdown is that you can <b>automate</b> your <b>reports</b> -- <p style = "margin-bottom:1cm;"> <center><h4>Why is it useful?</h4></center> <p style = "margin-bottom:1cm;"> <ul> <li>You might figure out quite late in the process that you need to <b>make a change</b> at the beginning of the analysis</li> <ul> <li>A change that potentially <b>impacts everything</b> that comes after in the report</li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- <ul> <li>Imagine that you forgot to filter out an irrelevant group of observations at the beginning</li> <ul> <li>If you simply filter your data at the beginning in a code chunk</li> <li>All your tables and figures will <b>update automatically</b></li> </ul> </ul> -- <p style = "margin-bottom:1cm;"> <ul> <li>But what if you wrote some of your results <b>within paragraphs?</b></li> <ul> <li>In a usual text formatting software you would have to update everything manually</li> <li>But here you can also make it <b>update automatically!</b></li> </ul> </ul> --- ### 2. Useful features #### 2.1. Inline code * Consider the following report : <center><img src = "report_example_1.png" width = "740"/></center> --- ### 2. Useful features #### 2.1. Inline code * Imagine that there is a problem with the observation for which `dist > 100` and that you should discard it <center><img src = "report_example_2.png" width = "740"/></center> --- ### 2. Useful features #### 2.1. Inline code <ul> <li>All the results were updated automatically but not the text</li> </ul> <p style = "margin-bottom:-.5cm;"> -- <ul> <ul> <li>That's where <b>inline code</b> comes in!</li> </ul> </ul> -- <p style = "margin-bottom:.75cm;"> ➜ **Inline code** allows to include the output of some **R code within text areas** of your report <p style = "margin-bottom:.75cm;"> -- * R code outside code chunks should be included between backticks: * Surrounding code with **backticks** in a text area will **change** the **format** to that of the code chunk * **Adding** the **`r`** letter right after the first backtick will **show** the **output** of the code instead of the code -- .pull-left[ <center> <h4> Syntax </h4> </center> ```r `paste("a", "b", sep = "-")` ``` ```r `r paste("a", "b", sep = "-")` ``` ] .pull-right[ <center> <h4> Output </h4> </center> `paste("a", "b", sep = "-")` <p style = "margin-bottom:1cm;"> a-b ] --- ### 2. Useful features #### 2.1. Inline code * With inline code, **paragraphs** also do **update automatically**: <center><img src = "report_example_3.png" width = "700"/></center> --- ### 2. Useful features #### 2.2. Tables * Displaying a table as a raw output can be unpleasant to read -- ```r head(mtcars) ``` ``` ## mpg cyl disp hp drat wt qsec vs am gear carb ## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 ## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 ## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 ## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 ## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 ## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 ``` -- <p style = "margin-bottom:1.25cm;"> * The `kable()` function from the `knitr` package allows to display tables in a nice way ```r library("knitr") ``` --- ### 2. Useful features #### 2.2. Tables * You just need to put the table you want to display inside the `kable()` function ```r kable(head(mtcars), caption = "First rows of the dataset") ``` -- <table class="table table-hover table-condensed" style="width: auto !important; margin-left: auto; margin-right: auto;"> <caption>First rows of the dataset</caption> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> mpg </th> <th style="text-align:right;"> cyl </th> <th style="text-align:right;"> disp </th> <th style="text-align:right;"> hp </th> <th style="text-align:right;"> drat </th> <th style="text-align:right;"> wt </th> <th style="text-align:right;"> qsec </th> <th style="text-align:right;"> vs </th> <th style="text-align:right;"> am </th> <th style="text-align:right;"> gear </th> <th style="text-align:right;"> carb </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Mazda RX4 </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.62 </td> <td style="text-align:right;"> 16.46 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Mazda RX4 Wag </td> <td style="text-align:right;"> 21.0 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 160 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.90 </td> <td style="text-align:right;"> 2.88 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 4 </td> </tr> <tr> <td style="text-align:left;"> Datsun 710 </td> <td style="text-align:right;"> 22.8 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 108 </td> <td style="text-align:right;"> 93 </td> <td style="text-align:right;"> 3.85 </td> <td style="text-align:right;"> 2.32 </td> <td style="text-align:right;"> 18.61 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 4 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet 4 Drive </td> <td style="text-align:right;"> 21.4 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 258 </td> <td style="text-align:right;"> 110 </td> <td style="text-align:right;"> 3.08 </td> <td style="text-align:right;"> 3.21 </td> <td style="text-align:right;"> 19.44 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> Hornet Sportabout </td> <td style="text-align:right;"> 18.7 </td> <td style="text-align:right;"> 8 </td> <td style="text-align:right;"> 360 </td> <td style="text-align:right;"> 175 </td> <td style="text-align:right;"> 3.15 </td> <td style="text-align:right;"> 3.44 </td> <td style="text-align:right;"> 17.02 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 2 </td> </tr> <tr> <td style="text-align:left;"> Valiant </td> <td style="text-align:right;"> 18.1 </td> <td style="text-align:right;"> 6 </td> <td style="text-align:right;"> 225 </td> <td style="text-align:right;"> 105 </td> <td style="text-align:right;"> 2.76 </td> <td style="text-align:right;"> 3.46 </td> <td style="text-align:right;"> 20.22 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 3 </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- ### 2. Useful features #### 2.2. Tables * For **big tables**, one solution is the `datatable()` function from the `DT` package <p style = "margin-bottom:1cm;"> -- * As with `kable()`, you just need to put the table you want to display inside the `datatable()` function ```r library("DT") datatable(mtcars) ``` -- <p style = "margin-bottom:1cm;"> <ul> <li>The output will be an <b>interactive table</b> which allows to:</li> <ul> <li>Navigate in the table by displaying a limited number of rows at a time</li> <li>Choose the number of rows to display</li> <li>Search for a given element in the table</li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- * You can select the default number of rows to display as follows ```r datatable(mtcars, options = list(pageLength = 5)) ``` --- ### 2. Useful features #### 2.2. Tables
-- <p style = "margin-bottom:1.5cm;"> <center><i>➜ Try to search for <b>"Toyota"</b> for instance</i></center> --- ### 2. Useful features #### 2.3. Preset themes <ul> <li>The <b>default theme</b> of R Markdown might seem <b>a bit dull</b></li> <ul> <li>The look of your reports can easily be <b>enhanced</b> using a variety of <b>preset</b> themes</li> <li>The preset theme to use should be specified in the <b>YAML header</b></li> <li>Add a theme argument to the html_document format specified as output</li> </ul> </ul> -- <p style = "margin-bottom:1cm;"> ````markdown --- title: "My First Markdown Report" author: "Louis Sirugue" date: "24/09/2021" output: html_document: theme: cosmo --- ```` <p style = "margin-bottom:1cm;"> -- <ul> <li>When using themes from downloaded packages, the way you set the theme can be slightly different</li> <ul> <li>Check the online documentation</li> </ul> </ul> --- ### 2. Useful features #### 2.3. Preset themes <p style = "margin-bottom:-1cm;"> .pull-left[ <center><h4 style = "margin-bottom:0cm;">cosmo</h4> <img src = "default_cosmo.png" width = "550"/></center> ] -- .pull-right[ <center><h4 style = "margin-bottom:0cm;">cerulean</h4> <img src = "default_cerulean.png" width = "550"/></center> ] --- ### 2. Useful features #### 2.3. Preset themes <p style = "margin-bottom:-1cm;"> .pull-left[ <center><h4 style = "margin-bottom:0cm;">cayman (from prettydoc)</h4> <img src = "prettydoc_cayman.png" width = "375"/></center> ] -- .pull-right[ <center><h4 style = "margin-bottom:0cm;">tactile (from prettydoc)</h4> <img src = "prettydoc_tactile.png" width = "360"/></center> ] --- ### 2. Useful features #### 2.3. Preset themes <center><h4 style = "margin-bottom:0cm;">leonids (from prettydoc)</h4> <img src = "prettydoc_leonids.png" width = "725"/></center> --- ### 2. Useful features #### 2.3. Preset themes <p style = "margin-bottom:-.5cm;"> .pull-left[ <center><h4 style = "margin-bottom:0cm;">downcute (from rmdformats)</h4> <img src = "rmdformats_downcute.png" width = "550"/></center> ] -- .pull-right[ <center><h4 style = "margin-bottom:.5cm;">downcute chaos (from rmdformats)</h4> <img src = "rmdformats_downcute_chaos.png" width = "550"/></center> ] --- ### 2. Useful features #### 2.3. Preset themes .pull-left[ <center><h4 style = "margin-bottom:0cm;">readthedown (from rmdformats)</h4> <img src = "rmdformats_readthedown.png" width = "550"/></center> ] -- .pull-right[ <center><h4 style = "margin-bottom:0cm;">robobook (from rmdformats)</h4> <img src = "rmdformats_robobook.png" width = "550"/></center> ] --- class: inverse, hide-logo ### Practice .pull-left[ <p style = "margin-bottom:2cm;"></p> <center><b><i>Reproduce the following html using R markdown</i></b></center> <p style = "margin-bottom:.25cm;"></p> <center><button onclick="copyText()">Copy raw output</button></center> <script> function copyText() { navigator.clipboard.writeText ("Report on the first name LOUIS \n Your name \n Fall 2022 \n \n 1. Setup \n \n The packages needed in an Rmd must always be loaded in a code chunk at the beginning of the file. \n \n library(dplyr) \n library(ggplot2) \n \n However, the command install.packages() must not be written in an R markdown. \n It should be run only once in the console. \n \n 2. Data cleaning \n \n names <- read.csv('fichier_prenoms.csv', encoding = 'UTF-8', sep = ';') %>% \n mutate(sexe = ifelse(sexe == 1, 'Male', 'Female')) %>% \n rename(Sex = sexe, Year = annais) %>% \n filter(Year != 'XXXX') %>% \n mutate(Year = as.numeric(Year)) \n \n 3. Evolution of the first name LOUIS over time \n \n 3715 children were born under the name LOUIS in 2021. This statistic is written in inline code such that it updates automatically."); } </script> <p style = "margin-bottom:2cm;"></p> <center><h3><i>You've got 15 minutes!</i></h3></center>
−
+
15
:
00
] .pull-right[ <p style = "margin-bottom:-4cm;"></p> <center><img src = "louis.png" height = "677" width = "auto"/></center> ] --- class: inverse, hide-logo ### Solution <p style = "margin-bottom:1.5cm;"></p> ````markdown --- title: "Report on the first name LOUIS" author: "Your name" date: "Fall 2022" output: html_document: theme: cosmo --- ### 1) Setup The packages needed in an Rmd must *always* be loaded in a code chunk at the beginning of the file. ```{r, message = F, warning = F} library(dplyr) library(ggplot2) ``` However, the command `install.packages()` must **not** be written in an R markdown. It should be run only once in the console. ```` --- class: inverse, hide-logo ### Solution <p style = "margin-bottom:-.5cm;"></p> ````markdown ### 2) Data cleaning ```{r} names <- read.csv("fichier_prenoms.csv", encoding = "UTF-8", sep = ";") %>% mutate(sexe = ifelse(sexe == 1, "Male", "Female")) %>% rename(Sex = sexe, Year = annais) %>% filter(Year != "XXXX") %>% mutate(Year = as.numeric(Year)) ``` ### 3) Evolution of the first name LOUIS over time ```{r, echo = F, message = F, fig.height = 3} names %>% filter(preusuel == "LOUIS") %>% group_by(Sex, Year) %>% summarise(Total = sum(nombre)) %>% ggplot(aes(x = Year, y = Total, color = Sex)) + geom_line() ``` `r sum(names[names$preusuel=='LOUIS' & names$Year==2021, 'nombre'])` children were born under the name LOUIS in 2021. This statistic is written in **inline code** such that it updates automatically ```` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>It may sometimes be useful to produce <b>separate html reports for differents groups</b> in your data</li> <ul> <li>Country/state-specific reports</li> <li>Here, a different report for each first name</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> -- <ul> <li><b>YAML parameters</b> are very useful for that</li> <ul> <li>They are accessible <b>like any object</b> in your environment</li> <li>They must be specified as follows</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> -- ```r *title: "Report on the first name `r params$name`" author: "Your name" date: "Fall 2022" output: html_document: theme: cosmo *params: * name: "LOUIS" ``` <p style = "margin-bottom:1cm;"></p> --- ### 2. Useful features #### 2.4. Report parameters * You simply have to call that object in your code chunks or inline code when needed -- <p style = "margin-bottom:1cm;"></p> ````markdown *### 3) Evolution of the first name `r params$name` over time ```{r, echo = F, message = F, fig.height = 3} names %>% * filter(preusuel == params$name) %>% group_by(Sex, Year) %>% summarise(Total = sum(nombre)) %>% ggplot(aes(x = Year, y = Total, color = Sex)) + geom_line() ``` *`r sum(names[names$preusuel == params$name & names$Year == 2021, 'nombre'])` children were born *under the name `r params$name` in 2021. This statistic is written in **inline code** such that it updates automatically. ```` -- <center><i><b>➜ Let's knit our .Rmd with different values of that parameter!</b></i></center> --- class: hide-logo .pull-left[ <p style = "margin-bottom:-.9cm;"></p> <center><img src = "louis.png" height = "677" width = "auto"/></center> ] -- .pull-right[ <p style = "margin-bottom:-.9cm;"></p> <center><img src = "didier.png" height = "677" width = "auto"/></center> ] --- class: hide-logo .pull-left[ <p style = "margin-bottom:-.9cm;"></p> <center><img src = "pauline.png" height = "677" width = "auto"/></center> ] -- .pull-right[ <p style = "margin-bottom:-.9cm;"></p> <center><img src = "camille.png" height = "677" width = "auto"/></center> ] --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>But by <b>default</b> the <b>name of the .html</b> output will be the name of your .Rmd</li> <ul> <li>So if you <b>knit report.Rmd</b> for the first name Louis it will save the report under <b>report.html</b></li> <li>And if you <b>knit it a second time</b> for the first name Didier it will <b>override</b> the first .html</li> </ul> </ul> <p style = "margin-bottom:1.25cm;"></p> -- <ul> <li>The <b>solution</b> is to <b>knit</b> your .Rmd <b>externally</b></li> <ul> <li>You can do that with the <b>render()</b> function of the rmarkdown package</li> <li>Save your .Rmd and <b>open a new .R script</b> to try it out</li> </ul> </ul> <p style = "margin-bottom:1.25cm;"></p> -- ```r library(rmarkdown) render( input = "C:/User/Documents/prenom.Rmd", # Specify the input .Rmd output_file = "C:/User/Documents/LOUIS.html", # Specify the output file params = list(name = "LOUIS") # Specify the YAML parameter(s) ) ``` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>To <b>avoid copy-pasting</b> this command for each name we want a report on, we must <b>use a loop</b></li> </ul> -- <ul style = "margin-top:-.53cm"> <ol> <li></li> <li></li> <li></li> <li></li> </ol> </ul> <p style = "margin-bottom:1.5cm;"></p> ```r for ( in ) { } ``` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>To <b>avoid copy-pasting</b> this command for each name we want a report on, we must <b>use a loop</b></li> <ol> <li>First we should name the object that will successively take the value of each first name</li> <li></li> <li></li> <li></li> </ol> </ul> <p style = "margin-bottom:1.5cm;"></p> ```r for (i in ) { } ``` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>To <b>avoid copy-pasting</b> this command for each name we want a report on, we must <b>use a loop</b></li> <ol> <li>First we should name the object that will successively take the value of each first name</li> <li>Then indicate which values this object must successively take</li> <li></li> <li></li> </ol> </ul> <p style = "margin-bottom:1.5cm;"></p> ```r for (i in c("LOUIS", "DIDER", "PAULINE", "CAMILLE")) { } ``` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>To <b>avoid copy-pasting</b> this command for each name we want a report on, we must <b>use a loop</b></li> <ol> <li>First we should name the object that will successively take the value of each first name</li> <li>Then indicate which values this object must successively take</li> <li>Then indicate what to do at each iteration</li> <li></li> </ol> </ul> <p style = "margin-bottom:1.5cm;"></p> ```r for (i in c("LOUIS", "DIDER", "PAULINE", "CAMILLE")) { render( input = "C:/User/Documents/prenom.Rmd", output_file = "C:/User/Documents/LOUIS.html", params = list(name = "LOUIS") ) } ``` --- ### 2. Useful features #### 2.4. Report parameters <ul> <li>To <b>avoid copy-pasting</b> this command for each name we want a report on, we must <b>use a loop</b></li> <ol> <li>First we should name the object that will successively take the value of each first name</li> <li>Then indicate which values this object must successively take</li> <li>Then indicate what to do at each iteration</li> <li>And this should depend on the object that successively take each value</li> </ol> </ul> <p style = "margin-bottom:1.5cm;"></p> ```r for (i in c("LOUIS", "DIDER", "PAULINE", "CAMILLE")) { render( input = "C:/User/Documents/prenom.Rmd", output_file = paste0("C:/User/Documents/", i, ".html"), params = list(name = i) ) } ``` --- <h3>Overview</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles ✔</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features ✔</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] .pull-right[ <ul style = "margin-left:-1cm;list-style: none"> <li><b>3. LaTeX for equations</b></li> <ul style = "list-style: none"> <li>3.1. What is LaTeX?</li> <li>3.2. LaTeX syntax</li> <li>3.3. Large equations</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:-1cm;list-style: none"><li><b>4. Wrap up!</b></li></ul> ] --- <h3>Overview</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles ✔</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features ✔</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] .pull-right[ <ul style = "margin-left:-1cm;list-style: none"> <li><b>3. LaTeX for equations</b></li> <ul style = "list-style: none"> <li>3.1. What is LaTeX?</li> <li>3.2. LaTeX syntax</li> <li>3.3. Large equations</li> </ul> </ul> ] --- ### 3. LaTeX for equations #### 3.1. What is LaTeX? * `\(\LaTeX\)` is a document preparation system <p style = "margin-bottom:.95cm;"> -- <ul> <li>But LaTeX is not a <i>"what you see is what you get"</i> system</li> <ul> <li>In Microsoft Word or Google doc, you work directly on the "output document"</li> <li><b>LaTeX</b> works more like R Markdown: <b>Edit</b> your text <b>in a script using commands and symbols</b></li> </ul> </ul> <p style = "margin-left:11.25cm; margin-top:-.55cm;"><b>Compile</b> the script to <b>get the output</b></p> <p style = "margin-bottom:.95cm;"> -- <ul> <li>LaTeX is the <b>preferred</b> typesetting system for most <b>academic</b> fields mainly because:</li> <ul> <li>Many things can be <b>automated</b> in LaTeX</li> <li>It has a good way to typeset <b>mathematical formulas</b></li> </ul> </ul> <p style = "margin-bottom:.95cm;"> -- * We're not gonna learn how to make `\(\LaTeX\)` documents ([do it in 30mn](https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes)), but just how to make equations `$$\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i$$` --- ### 3. LaTeX for equations #### 3.2. LaTeX syntax * To include a **LaTeX equation** in R Markdown, you simply have to surround it with the **`$` sign**: -- <p style = "margin-bottom:-.5cm;"> .pull-left[ <center> <h4> Syntax </h4> </center> `1 + 1` `$1 + 1$` ] .pull-right[ <h4> Output </h4> 1 + 1 `\(1 + 1\)` ] -- <p style = "margin-bottom:1.25cm;"> * LaTeX is a convenient way to display **mathematical symbols** and to **structure equations** * The **syntax** is mainly based on **backslashes \ and braces {}** <p style = "margin-bottom:1cm;"> -- <center><h4 style = "margin-bottom:.25cm;"> Example: </h4></center> <p style = "margin-bottom:.25cm;"> ➜ What you type in the text area: `$x \neq \frac{\alpha \times \beta}{2}$` </p> ➜ What is rendered when knitting the document: `\(x \neq \frac{\alpha \times \beta}{2}\)` --- ### 3. LaTeX for equations #### 3.2. LaTeX syntax ➜ **Common greek letters** <p style = "margin-bottom:-.25cm;"> .pull-left[ <center> <h4> Syntax </h4> </center> `$\alpha$` `$\beta$` `$\gamma$` `$\Gamma$` `$\delta$` `$\Delta$` `$\epsilon$` `$\varepsilon$` `$\lambda` `\Lambda$` `$\phi$` `$\Phi$` `$\pi$` `$\Pi$` `$\psi$` `$\Psi$` `$\theta$` `$\Theta$` `$\sigma$` `$\Sigma$` ... ] .pull-right[ <center> <h4> Output </h4> </center> `\(\alpha\)` `\(\beta\)` `\(\gamma\)` `\(\Gamma\)` `\(\delta\)` `\(\Delta\)` `\(\epsilon\)` `\(\varepsilon\)` `\(\lambda\)` `\(\Lambda\)` `\(\phi\)` `\(\Phi\)` `\(\pi\)` `\(\Pi\)` `\(\psi\)` `\(\Psi\)` `\(\theta\)` `\(\Theta\)` `\(\sigma\)` `\(\Sigma\)` ... ] --- ### 3. LaTeX for equations #### 3.2. LaTeX syntax ➜ **Common symbols** .pull-left[ <center> <h4> Syntax </h4> </center> `$+ - \pm$` `$\times \div$` `$= \neq \equiv \approx$` `$> < \geq \leq \lessgt$` `$\rightarrow \leftarrow \Leftrightarrow$` `$\in \notin$` `$\forall \exists \nexists$` `$\infty$` `$\sum \prod \int$` ... ] .pull-right[ <center> <h4> Output </h4> </center> `\(+\)` `\(-\)` `\(\pm\)` `\(\times\)` `\(\div\)` `\(=\)` `\(\neq\)` `\(\equiv\)` `\(\approx\)` `\(>\)` `\(<\)` `\(\geq\)` `\(\leq\)` `\(\lessgtr\)` `\(\rightarrow\)` `\(\leftarrow\)` `\(\Leftrightarrow\)` `\(\in\)` `\(\notin\)` `\(\forall\)` `\(\exists\)` `\(\nexists\)` `\(\infty\)` `\(\sum\)` `\(\prod\)` `\(\int\)` ... ] --- ### 3. LaTeX for equations #### 3.2. LaTeX syntax ➜ **Exponents and accentuation** .pull-left[ <center> <h4> Syntax </h4> </center> `$x^a$` `$x_b$` `$x^a_b$` `$x^{a, i}_{b, j}$` <p style = "margin-bottom:.25cm;"> `$\hat{\beta} \widehat{\beta_{i,j}}$` `$\tilde{\beta} \widetilde{\beta_{i,j}}$` `$\overline{x} \underline{x}$` `$\overrightarrow{x} \underleftarrow{x}$` ... ] .pull-right[ <center> <h4> Output </h4> </center> `\(x^a\)` `\(x_b\)` `\(x^a_b\)` `\(x^{a, i}_{b, j}\)` `\(\hat{\beta}\)` `\(\widehat{\beta_{i,j}}\)` `\(\tilde{\beta}\)` `\(\widetilde{\beta_{i,j}}\)` `\(\overline{x}\)` `\(\underline{x}\)` `\(\overrightarrow{x}\)` `\(\underleftarrow{x}\)` ... ] --- ### 3. LaTeX for equations #### 3.2. LaTeX syntax ➜ **Math constructs and variable sized symbols** .pull-left[ <center> <h4> Syntax </h4> </center> `$\frac{a \times b}{c}$` `$\sqrt{x} \sqrt[n]{x}$` `$\sum_{i = 1}^N$` `$\prod_{i = 1}^N$` `$\int_a^b$` <p style = "margin-bottom:.5cm;"> `$\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i$` ... ] .pull-right[ <center> <h4> Output </h4> </center> `\(\frac{a \times b}{c}\)` `\(\sqrt{x}\)` `\(\sqrt[n]{x}\)` `\(\sum_{i = 1}^N\)` `\(\prod_{i = 1}^N\)` `\(\int_a^b\)` `\(\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i\)` ... ] --- ### 3. LaTeX for equations #### 3.3. Large equations <ul> <li>Surrounding a LaTeX input with <b>one $</b> on each side is suitable for <b>inline equation</b></li> </ul> -- <p style = "margin-bottom:1cm;"> <ul> <li>You can also surround a LaTeX input with <b>two $</b> on each side</li> <ul> <li>It puts the equation at the <b>center of a new line</b></li> <li>And gives <b>more vertical space</b> to the equation</li> </ul> </ul> -- <p style = "margin-bottom:1cm;"> <ul> <li>Surrounding a LaTeX input with two $ is usually good for:</li> <ul> <li>Large equations</li> <li>Equations that should be emphasized</li> </ul> </ul> -- <p style = "margin-bottom:-.25cm;"> .pull-left[ <h4 style = "margin-bottom:-.5cm;">The mean formula with one `$` on each side</h4> ➜ For inline equations `\(\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i\)` ] .pull-right[ <h4 style = "margin-bottom:-.5cm;">The mean formula with two `$` on each side</h4> ➜ For large/emphasized equations `$$\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i$$` ] --- ### 3. LaTeX for equations #### 3.3. Large equations <ul> <li>Sometimes you do not want two <b>consecutive lines</b> of equations to be centered</li> <ul> <li>You may want to <b>align</b> them based on a <b>common part</b> within the equations</li> </ul> </ul> -- <p style = "margin-bottom:1cm;"> * This should be done in an **`aligned` environment** (`$\begin{aligned}...\end{aligned}$`) <p style = "margin-bottom:-.5cm;"> <ul> <ul> <li>Place the <b>"&"</b> symbol where the equations should be aligned</li> <li>And break a line using <b>"\\"</b></li> </ul> </ul> <p style = "margin-bottom:1cm;"> -- ```r $$ \begin{aligned} x & = (a + b) \times c \\ & = (a \times c) + (b \times c) \end{aligned} $$ ``` `$$\begin{aligned} x & = (a + b) \times c \\ & = (a \times c) + (b \times c) \end{aligned}$$` --- ### 3. LaTeX for equations #### 3.3. Large equations * The same principle applies within **`cases` environment** -- ```r $$\text{Med}(x) = \begin{cases} x[\frac{N+1}{2}] & \text{if } N \text{ is odd}\\ \frac{x[\frac{N}{2}]+x[\frac{N}{2}+1]}{2} & \text{if } N \text{ is even} \end{cases}$$ ``` `$$\text{Med}(x) = \begin{cases} x[\frac{N+1}{2}] & \text{if } N \text{ is odd}\\ \frac{x[\frac{N}{2}]+x[\frac{N}{2}+1]}{2} & \text{if } N \text{ is even} \end{cases}$$` -- * Note that the **`text` function** allows to write text without it being interpreted as mathematical letters: <p style = "margin-bottom:-.5cm;"> -- .pull-left[ ```r $$Mean(x)=\frac{1}{N}\sum_{i=1}^N x_i$$ ``` `$$Mean(x)=\frac{1}{N}\sum_{i=1}^N x_i$$` ] .pull-right[ ```r $$\text{Mean}(x)=\frac{1}{N}\sum_{i=1}^N x_i$$ ``` `$$\text{Mean}(x)=\frac{1}{N}\sum_{i=1}^N x_i$$` ] --- class: inverse, hide-logo ### Practice <p style = "margin-bottom:3cm;"></p> #### 1) Inside your .Rmd, reproduce the following mathematical expression `$$Y_i = \alpha + \beta X_i + \varepsilon_i$$` -- <p style = "margin-bottom:2cm;"></p> #### 2) Then reproduce the following sentence `\(\hat{Y_i}\)` denote the fitted values of the model. -- <p style = "margin-bottom:3cm;"></p> <center><h3><i>You've got 3 minutes!</i></h3></center>
−
+
03
:
00
--- class: inverse, hide-logo ### Solution <p style = "margin-bottom:3cm;"></p> #### 1) Inside your .Rmd, reproduce the following mathematical expression `$$Y_i = \alpha + \beta X_i + \varepsilon_i$$` -- ```r $$Y_i = \alpha + \beta X_i + \varepsilon_i$$ ``` -- <p style = "margin-bottom:2cm;"></p> #### 2) Then reproduce the following sentence `\(\hat{Y_i}\)` denote the fitted values of the model. -- ```r $\hat{Y_i}$ denote the fitted values of the model. ``` --- <h3>Overview</h3> <p style = "margin-bottom:2.5cm;"></p> .pull-left[ <ul style = "margin-left:1.5cm;list-style: none"> <li><b>1. Basic principles ✔</b></li> <ul style = "list-style: none"> <li>1.1. What is R Markdown?</li> <li>1.2. YAML header</li> <li>1.3. Code chunks</li> <li>1.4. Text formatting</li> <li>1.5. Run and knit your code</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:1.5cm;list-style: none"> <li><b>2. Useful features ✔</b></li> <ul style = "list-style: none"> <li>2.1. Inline code</li> <li>2.2. Tables</li> <li>2.3. Preset themes</li> <li>2.4. Report parameters</li> </ul> </ul> ] .pull-right[ <ul style = "margin-left:-1cm;list-style: none"> <li><b>3. LaTeX for equations ✔</b></li> <ul style = "list-style: none"> <li>3.1. What is LaTeX?</li> <li>3.2. LaTeX syntax</li> <li>3.3. Large equations</li> </ul> </ul> <p style = "margin-bottom:1cm;"></p> <ul style = "margin-left:-1cm;list-style: none"><li><b>4. Wrap up!</b></li></ul> ] --- ### 4. Wrap up! #### 1. Three types of contents .left-column[ <p style = "margin-bottom:1.5cm;"> <b> YAML header ➜</b> <p style = "margin-bottom:1.75cm;"> <b> Code chunks ➜</b> <p style = "margin-bottom:1.75cm;"> <b> Text ➜</b> ] .right-column[ <img src = "report_example_3.png" width = "700"/> ] --- ### 4. Wrap up! #### 2. Useful features ➜ **Inline code** allows to include the output of some **R code within text areas** of your report <p style = "margin-bottom:-.5cm;"> -- .pull-left[ <center> <h4> Syntax </h4> </center> ```r `paste("a", "b", sep = "-")` ``` ```r `r paste("a", "b", sep = "-")` ``` ] .pull-right[ <center> <h4> Output </h4> </center> `paste("a", "b", sep = "-")` <p style = "margin-bottom:1cm;"> a-b ] <p style = "margin-bottom:2cm;"> -- ➜ **`kable()`** for clean **html tables** and **`datatable()`** to navigate in **large tables** ```r kable(results_table) datatable(results_table) ``` --- ### 4. Wrap up! #### 3. LaTeX for equations * `\(\LaTeX\)` is a convenient way to display **mathematical** symbols and to structure **equations** * The **syntax** is mainly based on **backslashes \ and braces {}** -- <p style = "margin-bottom:1cm;"> ➜ What you **type** in the text area: `$x \neq \frac{\alpha \times \beta}{2}$` ➜ What is **rendered** when knitting the document: `\(x \neq \frac{\alpha \times \beta}{2}\)` -- <p style = "margin-bottom:1.5cm;"> <center>To <b>include</b> a <b>LaTeX equation</b> in R Markdown, you simply have to surround it with the <b>$ sign</b></center> <p style = "margin-bottom:0cm;"> .pull-left[ <h4 style = "margin-bottom:0cm;">The mean formula with one `$` on each side</h4> ➜ For inline equations `\(\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i\)` ] .pull-right[ <h4 style = "margin-bottom:0cm;">The mean formula with two `$` on each side</h4> ➜ For large/emphasized equations `$$\overline{x}=\frac{1}{N}\sum_{i=1}^N x_i$$` ]