class: center, middle, inverse, title-slide # Reproducible Workflow with
rmarkdown
## 😮 ### Tyson S. Barrett --- class: inverse # Outline -- ## What is "reproducibility"? -- ## How can `rmarkdown` help? -- ## Integrating `rmarkdown` into your workflow -- ## The Ins-and-Outs of `rmarkdown` --- # References Goodman, SN, Fanelli, D, Ioannidis, JPA. (2016). What does reproducibility really mean? Science Translational Medicine, 8(341), 1-6. --- class: inverse, middle, center # Reproducibility --- # Reproducibility
-- This class will focus on **Methods Reproducibility**. -- "Methods reproducibility refers to the provision of enough detail about study procedures and data so the same procedures could, in theory or in actuality, be exactly repeated," (Goodman et al., 2016). --- # Methods Reproducibility "Methods reproducibility refers to the provision of enough detail about study procedures and data so the same procedures could, in theory or in actuality, be exactly repeated," (Goodman et al., 2016). -- Requires - A replicable workflow. - Avoidance of input/output errors. - Documentation, documentation, documentation. - Transparent reporting. - A single raw data set from which all analyses are done. - Code (in this class, `R` code!) that works with the raw data. - Others? --- # Methods Reproducibility and Rmarkdown - A replicable workflow. - Avoidance of input/output errors. - Documentation, documentation, documentation. - Transparent reporting. - A single raw data set from which all analyses are done. - Code (in this class, `R` code!) that works with the raw data. - Others? -- ### Each can be aided by `rmarkdown` and its associated packages. -- ### We will demonstrate most of these throughout this course, but especially while we discuss `rmarkdown`. --- # Benefits of rmarkdown 1. Input (code) and output (tables/figures) are intimately tied---NO MANUAL (ERROR-PRONE) TABLES! 1. Document type is flexible and easy to adjust. 1. Updates to the analyses and output are straightfoward---no need to keep track of 50+ little things to change. -- 1. Provides quick reports that can be shared across many platforms (Word, Latex, PDF, HTML). 1. Makes transparent reporting easier---the actual steps of analysis are right next to the write up about the actual steps. 1. If we use version control (e.g. *Git*), the file system is much simpler. -- 1. It's cool. --- class: inverse, middle, center # Integrating rmarkdown into your workflow --- # If: - You are reporting findings - The analyses are iterative (report and re-analyze, report and re-analyze...) - Working with individuals that understand some Latex or Markdown ### Use Rmarkdown for the writing and reporting! -- ### It'll probably stretch you a bit as a researcher -- but that is a good thing. 🤗 --- class: middle, center # Get one started! ... --- class: inverse, middle, center # The Ins-and-Outs of rmarkdown --- class: middle, center # RStudio has a [great website](http://rmarkdown.rstudio.com/lesson-1.html) that we will use to understand rmarkdown better. --- # Main Pieces of RMarkdown ### `YAML` ### Code Chunks ### Plain text ### Inline Code --- # YAML **You Awesome Mammal Life** Tells R: 1. Title, author, date 1. Type of document 1. Other magic tricks .footnote[This is how you create a publishable report. We'll come back to it.] --- # Code Chunks These are treated as R code -- nothing more, nothing less. There are several options. Some important ones: 1. `echo` 1. `message` 1. `warning` 1. `results` 1. `fig.xxxx` 1. `eval` --- # Plain Text The text of the document. Use Markdown code here. .pull-left[ ### Equations ##### Inline `$ Y = \frac{1}{2} * X$` ##### Centered `$$ Y = \frac{1}{2} * X `$$` ] .pull-right[ ### Lists `1. item one` `2. item two` `- unordered item one` `- unordered item two` ### Images `![](path/to/image.png)` ] --- # Inline Code You can insert R code into the plain text. If we wanted to state the mean and SD of a variable, we can use: ```r `r mean(x)` `r sd(x)` ``` right in line with our plain text. --- class: inverse, middle, center # Some final notes --- background-image: url("https://www.rstudio.com/images/docs/projects_new.png") background-position: 50% 80% background-size: 600px # Easiest Way to Integrate RMarkdown --> RStudio Projects! --- class: inverse, middle, center # Pop Quiz Thing ## on the following slide --- # Do the Following 1. Open a new Rmarkdown file with a title, author, and date. 2. Set global rmarkdown options to `echo=TRUE`, `warning=FALSE`, `message=FALSE` 2. Output should be set to HTML. 3. Load the following data (you can download it by pressing the CSV button). 4. Print a summary table of the data (any summary that you'd like). 5. Knit the document.
--- class: inverse, middle, center # Tables and Figures in RMarkdown --- # Tables There are a few ways to get nice looking tables in Rmarkdown. 1. Output the console output (not as nice but still informative) 2. Output in HTML, Word, or Latex tables ### Good Packages <table> <thead> <tr> <th style="text-align:left;"> Package </th> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Output </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> knitr </td> <td style="text-align:left;"> kable() </td> <td style="text-align:left;"> Word, HTML, PDF </td> </tr> <tr> <td style="text-align:left;"> DT </td> <td style="text-align:left;"> datatable() </td> <td style="text-align:left;"> HTML </td> </tr> <tr> <td style="text-align:left;"> furniture </td> <td style="text-align:left;"> table1(), tableC() </td> <td style="text-align:left;"> Word, HTML, PDF </td> </tr> <tr> <td style="text-align:left;"> stargazer </td> <td style="text-align:left;"> stargazer() </td> <td style="text-align:left;"> PDF </td> </tr> <tr> <td style="text-align:left;"> texreg </td> <td style="text-align:left;"> texreg() </td> <td style="text-align:left;"> PDF </td> </tr> </tbody> </table> --- # Tables #### Text output with a few chunk options ``` Function Output knitr kable() Word, HTML, PDF DT datatable() HTML furniture table1(), tableC() Word, HTML, PDF stargazer stargazer() PDF texreg texreg() PDF ``` #### HTML output from kable() <table> <thead> <tr> <th style="text-align:left;"> Package </th> <th style="text-align:left;"> Function </th> <th style="text-align:left;"> Output </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> knitr </td> <td style="text-align:left;"> kable() </td> <td style="text-align:left;"> Word, HTML, PDF </td> </tr> <tr> <td style="text-align:left;"> DT </td> <td style="text-align:left;"> datatable() </td> <td style="text-align:left;"> HTML </td> </tr> <tr> <td style="text-align:left;"> furniture </td> <td style="text-align:left;"> table1(), tableC() </td> <td style="text-align:left;"> Word, HTML, PDF </td> </tr> <tr> <td style="text-align:left;"> stargazer </td> <td style="text-align:left;"> stargazer() </td> <td style="text-align:left;"> PDF </td> </tr> <tr> <td style="text-align:left;"> texreg </td> <td style="text-align:left;"> texreg() </td> <td style="text-align:left;"> PDF </td> </tr> </tbody> </table> --- # Tables There are some add-ons for the packages, such as the `kableExtra` package that let's you make the kable table's look better. -- ### Some best practices: 1. Only print what you want to print (e.g., remove any `summary(df)`'s) 2. Only make it as pretty as is necessary (don't waste time making something look slightly better when the slightly better is not necessary) 3. Keep the tables small (long tables are not generally very useful to an audience) 4. If you use `kable()` or similar functions, provide a caption for the table 5. Don't be redundant in your table. Use the space for providing unique pieces of information. --- # Figures ### Automatically including nice figures is a huge plus to using Rmarkdown There are several code chunk options for the figures: `fig.xxxx` options. ### If you want more customizability: .large[Can save the figure and use it within the same knitting process.] --- class: inverse, middle, center # Examples --- # For more information about the code chunks: .huge[ [https://yihui.name/knitr/options/](https://yihui.name/knitr/options/) ] --- # YAML and Templates Each type of document (Word, PDF, HTML) is entirely customizable ### HTML If you are familiar with CSS code, then you can do a ton with the HTML document type There are also downloadable templates that you can use .footnote[All these are best packaged within an Rstudio Project!] --- # YAML and Templates Each type of document (Word, PDF, HTML) is entirely customizable ### Word Templates Each Word document has specific styles that are default You can change this and use that as the default look of your Word documents .footnote[All these are best packaged within an Rstudio Project!] --- # YAML and Templates Each type of document (Word, PDF, HTML) is entirely customizable ### PDF This is where I have the most experience (see my [dissertation proposal](http://tysonbarrett.com/assets/Manuscript/MarginalMediation_Proposal.pdf)). I'll walk you through my dissertation project, including 1. The template file 2. The YAML code to use it 3. Other files I use .footnote[All these are best packaged within an Rstudio Project!] --- class: inverse, middle, center # Questions about RMarkdown?