class: center, middle, inverse, title-slide # Lab 06: Building reports in R --- layout: true <div class="my-footer"> <span> Dr. Lucy D'Agostino McGowan </span> </div> --- # Agenda * **Knitting a pdf document** * **Creating tables in R** * **Lab 06:** on your own --- ## Knitting a pdf document * Currently your yaml at the top of your `.Rmd` files looks something like this: ```yaml --- title: "The title of your document" name: "Your name" output: html_document --- ``` --- ## Knitting a pdf document .question[ What do you think you would change to render this as a **pdf** document instead of **html**? ] * Currently your yaml at the top of your `.Rmd` files looks something like this: ```yaml --- title: "The title of your document" name: "Your name" output: html_document --- ``` --- ## Knitting a pdf document .question[ What do you think you would change to render this as a **pdf** document instead of **html**? ] * Currently your yaml at the top of your `.Rmd` files looks something like this: ```yaml --- title: "The title of your document" name: "Your name" *output: pdf_document --- ``` -- * For this lab and your final project I want you to create .pdf files. --- ## Making pretty tables * We've been using the `tidy()` and `glance()` functions to print tables of our model output. * These kind of look like "code" in our final reports * We can make these prettier with the **knitr** package ```r lm(wt ~ am, data = mtcars) %>% tidy() ``` ``` ## # A tibble: 2 x 5 ## term estimate std.error statistic p.value ## <chr> <dbl> <dbl> <dbl> <dbl> ## 1 (Intercept) 3.77 0.165 22.9 1.49e-20 ## 2 am -1.36 0.258 -5.26 1.13e- 5 ``` --- ## Making pretty tables * We've been using the `tidy()` and `glance()` functions to print tables of our model output. * These kind of look like "code" in our final reports * We can make these prettier with the **knitr** package -- * Load the **knitr** package with `library(knitr)` * Use the `kable()` function to output a pretty table ```r *library(knitr) lm(wt ~ am, data = mtcars) %>% tidy() %>% * kable() ``` <table> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 3.768895 </td> <td style="text-align:right;"> 0.1646171 </td> <td style="text-align:right;"> 22.894914 </td> <td style="text-align:right;"> 0.00e+00 </td> </tr> <tr> <td style="text-align:left;"> am </td> <td style="text-align:right;"> -1.357895 </td> <td style="text-align:right;"> 0.2582726 </td> <td style="text-align:right;"> -5.257603 </td> <td style="text-align:right;"> 1.13e-05 </td> </tr> </tbody> </table> --- ## Putting all the R code at the end * For these fancy reports, we want to see the R code, but we don't want it intersperced throughout the document. * You can "hide" an R chunk by adding `echo = FALSE` to each of your chunks. ```` ```{r, echo = FALSE} ``` ```` * You can "hide" all of your chunks by adding this to the top of your .Rmd file ```eval library(knitr) opts_chunk$set(echo = FALSE) ``` -- * **THEN** at the end of you document (in the Appendix) you can add ```` ```{r ref.label=knitr::all_labels(), echo = TRUE, eval = FALSE} ``` ```` --- ## Open `Lab 06: Building Reports` in RStudio