options(htmltools.dir.version = FALSE)
# see: https://github.com/yihui/xaringan
# install.packages("xaringan")
# see: 
# https://github.com/yihui/xaringan/wiki
# https://github.com/gnab/remark/wiki/Markdown
options(width=110)
options(digits = 4)
knitr::opts_chunk$set(comment=NA, fig.width=6, fig.height=6, echo = TRUE, eval = TRUE, message = FALSE, warning = FALSE, fig.align = 'center')
library(tidyverse)
library(knitr)

What is a dynamic report?

.pull-left4[

A dynamic report is a document that contains key elements which are automatically accessed and/or created by code:

]

.pull-right4[

Example: http://rpubs.com/fabiorocha5150/decisiontreemodel

]


Why are dynamic reports important?

.pull-left3[

1. Efficiency

2. Reproducibility

3. Scaleability

]

.pull-right3[

knitr::include_graphics("images/reproducibility_images.png")

]


What are the benefits of dynamic reports?

.pull-left3[

]

.pull-right3[

]


What dynamic reports can I make in RStudio?

.pull-left3[

How? Answer: R Markdown

]

.pull-right3[

knitr::include_graphics("images/RMarkdownOutputFormats.png")

]


What is Markdown?

knitr::include_graphics("images/markdown_ss.png")

Cheatsheet

knitr::include_graphics("images/markdown_cheat.png")

Source: https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf


How do I write an R Markdown Document

knitr::include_graphics("images/openmarkdown.png")

R Markdown = Markdown + R code

knitr::include_graphics("images/markdown_example.png")

R Markdown = Markdown + R code

knitr::include_graphics("images/rmarkdown_ss2.png")

R Markdown = Markdown + R code

knitr::include_graphics("images/knitto.png")

R Markdown Applications

http://rmarkdown.rstudio.com/gallery.html


R Chunks in R Markdown

knitr::include_graphics("images/chunkoptions.png")

R Chunks in R Markdown

# Example: Set default values for ALL future chunks
#   with knitr::ops_chunk$set


knitr::opts_chunk$set(fig.width = 6,        # Figure width (in)
                      fig.height = 6,       # Figure height (in)
                      echo = TRUE,          # Repeat code
                      eval = TRUE,          # Evaluate chunks
                      message = FALSE,      # Don't print messages
                      warning = FALSE,      # Don't print warnings
                      fig.align = 'center') # Center figures

Inline chunks



knitr::include_graphics("images/minichunk_ss_D.png")

Inline chunks

knitr::include_graphics("images/minichunk_big_ss.png")

Key points in R Markdown

.pull-left3[



  1. You can easily read in external files like images, R code, or datasets.

  2. All external files should be in a folder in the "Root" Directory containing the Markdown (.Rmd) file

  3. All data necessary for your report must be explicitly read into the document. So keep them close by!

]

.pull-right3[

Here is the "Root" directory of the R Markdown file dynamicreports.Rmd that created this presentation

knitr::include_graphics("images/rmarkdown_directory.png")

]


Rendering output with knitr

.pull-left3[

knitr::include_graphics("images/knitr_hex.png")

| Function|Output| |:------|:----| | include_graphics(path)| Include an external image (e.g.; .png, .jpg)| | kable(df, format)|Include a dataframe as a table|

]

.pull-right3[

Print a dataframe as a table with kable()

kable(economics[1:3, c("date", "pop")], 
      format = 'markdown')

Include an image

include_graphics(path = "images/rlogo.png")

]


Key points in R Markdown

.pull-left3[



  1. When you "Knit" an R markdown document, it will start with an empty workspace (ie. it will forget everything!)

  2. You must explicitly load all packages with library() and load in external datasets (e.g.; clinical_data <- read_csv("data/data.csv"))

  3. If you have any typos, errors, or missing code, the document will not knit (this is a good thing!)

]

.pull-right3[

Well formatted R Markdown document

knitr::include_graphics("images/wellformattedmarkdown.png")

]


Output types

.pull-left3[

| Package|Description| |:------|:----| | xaringan| Slideshows (like this one!)| | papaja|APA Manuscripts| | rmdformats|Many templates| | prettydoc|Many templates|

]

.pull-right3[

R Markdown templates in R Studio

knitr::include_graphics("images/rmarkdown_templates.png")

]


To write to PDF, you need TeX installed

.pull-left3[




]

.pull-right3[

knitr::include_graphics("images/latex_collage.png")

]


Why are RMarkdown documents so great?

.pull-left3[



  1. The data, code, and output are all in the same place.

  2. Everything works and is replicable! (If it wasn't, the document wouldn't Knit!)

  3. You can produce great looking documents, from simple PDFs, to webpages, to presentations (like this one), to books.

]

.pull-right3[

knitr::include_graphics("images/markdown_complete.png")

]


R Packages - The ultimate dynamic report?

.pull-left3[

If you want a fully contained, dynamic report that contains data, code, statistics, plots, text, help files and documentation that you can easily share, your answer may be creating an R Package.

Installing an example package from GitHub

# Install cStudy2017 from github
install_github("bootcamp/cStudy2017")

# Load cStudy2017 package
library("cStudy2017")

# Open cStudy2017 package help
?cStudy2017

# Oh study1_A contains study 1...
ggplot(data = study1_A, 
       ...)

FFTrees(success ~ .,
        data = study1_A)

]

.pull-right3[

Folder structure for a simple package

knitr::include_graphics("images/phillips2017_package_ss.png")


Read Wickham's book "R Packages" (also available for free online) to learn how to write your own package

]


Practical



therbootcamp/BaselRBootcamp2017 documentation built on May 3, 2019, 10:45 p.m.