class: center, middle, inverse, title-slide # Building Reports in R Markdown ### Dr. D’Agostino McGowan --- layout: true <div class="my-footer"> <span> Dr. Lucy D'Agostino McGowan </span> </div> --- ## Harnessing the power of RMarkdown! * Code chunk options * Tables * Inline code * Output Formats --- ## Code chunk options * All chunk options can be found on [Yihui's website](http://yihui.name/knitr/options/) -- * ````{r}` -- * ````{r, echo = FALSE}` -- * ````{r, eval = FALSE}` -- * ````{r, message = FALSE, warning = FALSE}` -- * ````{r, fig.cap = "PUT CAPTION HERE"}` --- ## Code Chunks * For these fancy reports, we want to see the R code, but we don't want it interspersed throughout the document. * You can "hide" all of your chunks by adding this to the top of your .Rmd file .small[ ```` ```{r, echo = FALSE} library(knitr) opts_chunk$set(echo = FALSE) ``` ```` ] -- * **THEN** at the end of you document (in the Appendix) you can add .small[ ```` ```{r ref.label=knitr::all_labels(), echo = TRUE, eval = FALSE} ``` ```` ] --- class: inverse ## <svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 640 512"><path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"/></svg> `Application Exercise` * Choose one of your analyses from the last Lab and copy it into a new R Markdown file * Edit the chunks so that: * no messages or warnings are printed * All figures have appropriate captions * The code all is printed at the end of the document --- ## Tables * There are several packages for making pretty tables in RMarkdown -- * [knitr]((https://cran.r-project.org/web/packages/knitr/) * [xtable](https://cran.r-project.org/web/packages/xtable/) * [stargazer](https://cran.r-project.org/web/packages/stargazer/) * [pander](http://rapporter.github.io/pander/) * [tables](https://cran.r-project.org/web/packages/tables/) * [ascii](http://eusebe.github.io/ascii/) --- ## Tables with knitr ```r library(knitr) kable(mtcars[1:4, ], caption = "Look at all those cars!") ``` Table: Look at all those cars! | | 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| --- ## broom * Helps create "tidy" tables from your `lm` output -- .pull-left[ .small[ ```r lm(wt ~ am, data = mtcars) ``` ``` ## ## Call: ## lm(formula = wt ~ am, data = mtcars) ## ## Coefficients: ## (Intercept) am ## 3.769 -1.358 ``` ] ] .pull-right[ .small[ ```r library(broom) m <- lm(wt ~ am, data = mtcars) tidy(m) ``` ``` ## # 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 ``` ] ] --- ## kable + broom * Use the `kable()` function to output a pretty table .small[ ```r *library(knitr) m <- lm(wt ~ am, data = mtcars) kable(tidy(m)) ``` ] .small[ <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> ] --- class: inverse ## <svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 640 512"><path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"/></svg> `Application Exercise` * Using the same document as the previous application exercise, update all model output to be in "nicely" formatted tables using `tidy` and `kable` (or a different table package of your choosing) --- ## Inline code * You can include _inline_ by using `r` -- * This is especially useful if you want to reference a numeric value. --- class: inverse ## <svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 640 512"><path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"/></svg> `Application Exercise` * Using the same document as the previous application exercise, update all references to numeric values using inline code. _Be sure to round numbers_. --- ## Output formats ## Updating your `yaml` ```yaml --- title: "The title of your document" name: "Your name" *output: pdf_document --- ``` --- ## Output formats ## Updating your `yaml` ```yaml --- title: "The title of your document" name: "Your name" *output: word_document --- ``` --- class: inverse ## <svg style="height:0.8em;top:.04em;position:relative;" viewBox="0 0 640 512"><path d="M624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"/></svg> `Application Exercise` * Using the same document as the previous application exercise, update your file to output a word document.