class: center, middle, hide-logo <style type="text/css"> .remark-slide-content { font-size: 32px; font-family: 'optima', sans-serif; } h1 { font-family: 'optima', sans-serif; font-size: 30px; background-color: #8F9CB9; color: white; font-weight: bold; text-align:center; padding: 22px; /* padding will fill in color to edges*/ } h2 { font-family: 'optima', sans-serif; background-color: #8F9CB9; color: white; font-size: 18px; font-weight: bold; padding: 10px; /* padding will fill in color to edges*/ } h3 { font-family: 'optima', sans-serif; background-color: #8F9CB9; color: white; font-size: 14px; font-weight: bold; padding: 0px; /* padding will fill in color to edges*/ } h4 { font-family: 'optima', sans-serif; background-color: #bcc5d8; color: black; font-size: 13px; font-weight: bold; padding: 0px; /* padding will fill in color to edges*/ } .footnote { text-align: right; position: absolute; bottom: 5px; right: 35px; padding-right: 4em; font-size: 75%; } .center2 { margin: 0; position: absolute; top: 57%; left: 50%; -ms-transform: translate(-50%, -50%); transform: translate(-50%, -50%); } </style> ## Introduction to RMarkdown ### Greg Rousell <img src = "https://researchcentres.wlu.ca/ccrla/learning/assets/images/LSIRM_colour_logo-small1.png"> --- class: left ## Introduction to R Markdown ### Agenda - Introductions & Objectives - Why RMarkdown? - Basics - PDF - HTML - Word - Online Dashboards --- ## Introductions & Objectives - Name and organization - Experience with R, RStudio - What do you hope to get out of today --- ## GitHub - All the materials are hosted on my GitHub page: https://github.com/grousell/Intro-RMarkdown --- ## Why RMarkdown? - One language is translated to different languages (HTML, LaTex, Word) - Provides environment for data anaysis and notes - Creates high quality reports for distribution - Easily reproduce analyses and reports for different groups --- ## Getting Started .center2[ <img src="images/start_document.png" width="90%" /> ] --- ## Getting Started .center2[ <img src="images/start_document_2.png" width="90%" /> ] --- ## Basics .pull-left[ `*Italics*` `**Bold**` `***Bold and Italics***` `'Inline Code'` `# First Level Heading` `# Second Level Heading` `` ] .pull-right[ *Italics* **Bold** ***Bold and Italics*** `Inline Code` ### First Level Heading #### Second Level Heading  ] --- ## Lists * Unordered lists are started with `*` or `-` * Ordered lists begin with a number * Sublists are created by indenting 4 spaces, or tab .pull-left[ <img src = "images/list_1.png" width = 50%/> ] .pull-right[ <img src = "images/list_2.png"/> ] --- ## Practice * Open RStudio and a new RMarkdown file to create a PDF. * Click "Run" and knit the file * Change some text and knit again * Try different formats (HTML, Word, PowerPoint) --- ## Adding R Code .pull-left[ <img src = "images/insert_r_chunk.png"/> ] .pull-right[ <img src = "images/insert_r_chunk_2.png"/> ] --- ## Adding R Code .center2[ <img src="images/insert_r_chunk_3.png" width="120%" /> ] --- ## Charts ```r chart <- df |> count (session_useful) |> mutate (perc = n / sum (n)) |> ggplot (aes (session_useful, perc)) + geom_col() + scale_y_continuous(labels = scales::percent_format(accuracy = 1), limits = c(0,0.5)) + geom_text (aes (label = scales::percent(perc)), vjust = -1) + labs (title = "I found the session useful", x = "", y = "Percent") ``` --- ## Charts <img src="index_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- ## Charts .pull-left[ <img src = "images/chart_1.png" height=180px/> ] .pull-right[ <!-- --> ] --- class: left ## Charts <img src = "images/chart_2.png" height=180px/> <!-- --> --- ## Practice * In practice rmd file, add a new chuck of R code and read in some data * df <- read_csv ("https://t.ly/7hzC") * Create quick plot and insert into document --- ## Tables ```r table <- df |> count (session_useful) |> rename (`I found the session useful` = session_useful) |> mutate (Percent = scales::percent (n / sum (n)) ) table ``` ``` ## # A tibble: 5 × 3 ## `I found the session useful` n Percent ## <fct> <int> <chr> ## 1 1 - Not at All 8 2.9% ## 2 2 25 9.1% ## 3 3 39 14.2% ## 4 4 119 43.3% ## 5 5 - Significantly 84 30.5% ``` --- ## Tables ```r table |> kable() ``` <table> <thead> <tr> <th style="text-align:left;"> I found the session useful </th> <th style="text-align:right;"> n </th> <th style="text-align:left;"> Percent </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 - Not at All </td> <td style="text-align:right;"> 8 </td> <td style="text-align:left;"> 2.9% </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:left;"> 9.1% </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:right;"> 39 </td> <td style="text-align:left;"> 14.2% </td> </tr> <tr> <td style="text-align:left;"> 4 </td> <td style="text-align:right;"> 119 </td> <td style="text-align:left;"> 43.3% </td> </tr> <tr> <td style="text-align:left;"> 5 - Significantly </td> <td style="text-align:right;"> 84 </td> <td style="text-align:left;"> 30.5% </td> </tr> </tbody> </table> --- ## Tables: `kableExtra` http://haozhu233.github.io/kableExtra/awesome_table_in_html.html ```r table |> kable() |> kable_styling(position = "center", font_size = 25) ``` <table class="table" style="font-size: 25px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> I found the session useful </th> <th style="text-align:right;"> n </th> <th style="text-align:left;"> Percent </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 - Not at All </td> <td style="text-align:right;"> 8 </td> <td style="text-align:left;"> 2.9% </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:right;"> 25 </td> <td style="text-align:left;"> 9.1% </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:right;"> 39 </td> <td style="text-align:left;"> 14.2% </td> </tr> <tr> <td style="text-align:left;"> 4 </td> <td style="text-align:right;"> 119 </td> <td style="text-align:left;"> 43.3% </td> </tr> <tr> <td style="text-align:left;"> 5 - Significantly </td> <td style="text-align:right;"> 84 </td> <td style="text-align:left;"> 30.5% </td> </tr> </tbody> </table> --- ## Tables: `kableExtra` ```r table |> kable(align = c ("lcc")) |> kable_styling(font_size = 25) |> column_spec(2:3, width = "8em") ``` <table class="table" style="font-size: 25px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> I found the session useful </th> <th style="text-align:center;"> n </th> <th style="text-align:center;"> Percent </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 - Not at All </td> <td style="text-align:center;width: 8em; "> 8 </td> <td style="text-align:center;width: 8em; "> 2.9% </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:center;width: 8em; "> 25 </td> <td style="text-align:center;width: 8em; "> 9.1% </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:center;width: 8em; "> 39 </td> <td style="text-align:center;width: 8em; "> 14.2% </td> </tr> <tr> <td style="text-align:left;"> 4 </td> <td style="text-align:center;width: 8em; "> 119 </td> <td style="text-align:center;width: 8em; "> 43.3% </td> </tr> <tr> <td style="text-align:left;"> 5 - Significantly </td> <td style="text-align:center;width: 8em; "> 84 </td> <td style="text-align:center;width: 8em; "> 30.5% </td> </tr> </tbody> </table> --- ## Tables: `kableExtra` ```r table |> mutate (Percent = cell_spec (Percent, background = "#FA19C3"), n = ifelse (n > 40, cell_spec (n, color = "green"), cell_spec (n, color = "red"))) |> kable(escape = FALSE, align = c ("lcc")) |> kable_styling(font_size = 25) |> column_spec(2:3, width = "8em") ``` --- ## Tables: `kableExtra` <table class="table" style="font-size: 25px; margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> I found the session useful </th> <th style="text-align:center;"> n </th> <th style="text-align:center;"> Percent </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1 - Not at All </td> <td style="text-align:center;width: 8em; "> <span style=" color: red !important;">8</span> </td> <td style="text-align:center;width: 8em; "> <span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FA19C3 !important;">2.9%</span> </td> </tr> <tr> <td style="text-align:left;"> 2 </td> <td style="text-align:center;width: 8em; "> <span style=" color: red !important;">25</span> </td> <td style="text-align:center;width: 8em; "> <span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FA19C3 !important;">9.1%</span> </td> </tr> <tr> <td style="text-align:left;"> 3 </td> <td style="text-align:center;width: 8em; "> <span style=" color: red !important;">39</span> </td> <td style="text-align:center;width: 8em; "> <span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FA19C3 !important;">14.2%</span> </td> </tr> <tr> <td style="text-align:left;"> 4 </td> <td style="text-align:center;width: 8em; "> <span style=" color: green !important;">119</span> </td> <td style="text-align:center;width: 8em; "> <span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FA19C3 !important;">43.3%</span> </td> </tr> <tr> <td style="text-align:left;"> 5 - Significantly </td> <td style="text-align:center;width: 8em; "> <span style=" color: green !important;">84</span> </td> <td style="text-align:center;width: 8em; "> <span style=" border-radius: 4px; padding-right: 4px; padding-left: 4px; background-color: #FA19C3 !important;">30.5%</span> </td> </tr> </tbody> </table> --- ## Tables Other packages include: - `flextable` - `gt` - `formattable` --- ## Practice * In practice rmd file, add a new chuck of R code and insert table into document * Add some custome formatting * https://bit.ly/kable_extra * https://bit.ly/flextable --- ## Split Columns <img src = "images/multi_cols.PNG" width = 120% class = "center"/> --- ## Split Columns <img src = "images/multi_cols_2.PNG" width = 120% class = "center"/> --- ## Split Columns .pull-left[ The figure on the right shows Horsepower vs Miles per Gallon. ] .pull-right[ <img src = "images/plot.png"/> ] --- ## Multiple Reports - One File ```r for (sch in unique(df$school)) { rmarkdown::render("Advanced_PDF_Example.Rmd", output_file = paste(sch, "_report.pdf", sep=''), output_dir = './reports/') } ``` --- ## Practice * Create custom report using multi-columns * Try loop to create multiple reports --- ## Questions?