knitr::opts_chunk$set(comment=NA, fig.width=6, fig.height=6, echo = FALSE, eval = FALSE, message = FALSE, warning = FALSE, fig.align = 'center', out.width = "100%")
knitr::include_graphics("images/rmarkdown_hex.png")

Slides

Here are the introduction slides for this practical on dynamic reports!

Overview

In this practical you'll practice creating interactive reports using RMarkdown.

Cheatsheet

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

If you don't have it already, you can access the Markdown cheatsheet here https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf

Examples

knitr::include_graphics("images/rmarkdown_ss_A.png")
knitr::include_graphics("images/rmarkdown_ss_B.png")
knitr::include_graphics("images/rmarkdown_ss_C.png")
knitr::include_graphics("images/rmarkdown_ss_D.png")
knitr::include_graphics("images/rmarkdown_ss_E.png")

Tasks

  1. Create a new R project called dynamicreports.Rproj. Add four folders 1_Data, 2_Code, 3_Materials.

  2. Go through the Examples above to create a new R Markdown document. Save the document under the name speffanalysis.Rmd in the root directory of your project (that is, next to the dynamicreports.Rproj file.

  3. At the top of the speffanalysis.Rmd document, add a new R chunk. You can do this by clicking the "Insert" button at the top of the console, or by using the "Command + Option + i" shortcut.

  4. In the chunk options, include echo = FALSE, message = FALSE, warning = FALSE. Inside of the chunk include the following code to set your global chunk options:

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


options(digits = 2)  # Round all output to 2 digits
  1. Now create another chunk. Inside this chunk, write the comment # Loading Packages -------------. Then, using the library() function, load the packages tidyverse, knitr and speff2trial.

  2. Knit the document to make sure it worked! If you have any errors, try to figure out how to solve them!

  3. For this practical we'll use the ACTG175 dataset. This dataset originally comes from the speff2trial package. However, I saved a copy of the dataset as a text file at "https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_slides/data/ACTG175.csv". Open this link in a web-browser, and then save the ACTG175.csv file to the 1_Data folder.

  4. Create a new code chunk and put it under the previous one. We will use this chunk to load the ACTG175.csv. In this chunk, read the csv data with read_csv() and assign the result to the object ACTG175. Write appropriate comments in the chunk!

  5. Knit the document! Diagnose and correct any errors!

  6. Add the necessary text and markdown to your document to create the following two paragraphs. Pay attention to the header sizes, italics and code formats.

knitr::include_graphics("images/markdown_ss.png")
  1. Knit the document! Diagnose and correct any errors!

  2. Add the appropriate combination of text, markdown, code chunks, and R code to add the following output to your document. To report the number of patients, use an in-line chunk to access the number directly from the data -- that is, don't type 2139 directly! To create the table, create a new chunk, and inside that chunk, use the kable() function, with the appropriate arguments, to create the table.

knitr::include_graphics("images/markdown_analysis_ss.png")
  1. Knit the document! Diagnose and correct any errors!

  2. Write the necessary code to add the following output to your document. To do this, create a new chunk. In the chunk use dplyr code to create the summary table of data. Assign the result to the object trial_summary. Then, use kable() to render this dataframe as a table in the final document.

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

Here is some code you might find helpful in creating this table!

# Helpful code to create the summary table!

tbl2 <- ACTG175 %>% 
  group_by(arms) %>% 
  summarise(
    N = n(),
    Mean = mean(days),
    Median = median(days),
    SD = sd(days),
    Max = max(days)
)
  1. Knit the document! Diagnose and correct any errors!

  2. Add the appropriate combination of text, markdown, code chunks, and R code to add the following output to your document. Be sure to include the figure caption (you can do this with the fig.cap argument to the chunk)

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

This code might help you to create the plot:

# Boxplot code template

ggplot(data = XX, 
       mapping = aes(x = factor(XX), y = XX)) +
  geom_boxplot() + 
  labs(x = "Treatment Arm",
       y = "Number of days until a major negative event",
       title = "ACTG175",
       subtitle = "Created within an RMarkdown Document!",
       caption = "Source: speff2trial R package") + 
  theme_bw()
  1. Knit the document! Diagnose and correct any errors!

  2. Add a new section called "Conclusions". Write the main conclusions of your analyses in one or two sentences. Feel free to add some formatting and/or in-line chunks to your content!

  3. You can easily publish an HTML document online to Rpubs.com for free. To do this, Knit your document. Then, click the blue "Publish" button. Go through the process of signing up for a free Rpubs account and get your document online!

Slideshow

  1. Now it's time to create a slideshow! To do this, we'll start with one of the templates in RStudio. Click File -- New File -- R Markdown. Then select "Presentation". Give the presentation a title and your name as the author. Then click ok.

  2. You should see a new .Rmd document open. Save the document in your main directory as slideshow.Rmd.

  3. Knit the document to see the outline of the presentation!

Note: If you don't like the look of the default presentation, you can also try a Ninja presentation click here for a demo from the xaringan package (that's what we use for all of our BaselRBootcamp slides). To install the xaringan package from GitHub, run the following code. Then open a new template with File -- New File -- R Markdown -- From Template -- Ninja Presentation.

# Install the xaringan package from github
devtools::install_github("yihui/xaringan")
  1. Play around with the presentation a bit. Change the existing content a bit and add a few slides. Try adding an image (maybe this one: https://actgnetwork.org/sites/default/files/images/ACTG_logo_2007_color.jpg) by saving the image to your 3_Materials folder, and then loading the image into your document with include_graphics().

  2. Now, try to customize the presentation to include all of main analyses, outputs, and plots you have in your speffanalysis.Rmd document! Of course, there won't be room for all of the text, so treat it like a normal presentation and put in what's important.

Challenges

C1. A researcher wants to know if there is a correlation between patients' CD4 T cell count at baseline (cd40) and the number of days until a major negative event. Include this information as a new subsection (with a second level header) in your analyses. To do this, run the following chunk. Then, write a sentence with the main outputs from the test, using inline chunks to directly access the correlation and the p-value. For example, a sentence could be: "The correlation between CD4 T cell count at baseline and number of days until a major negative event was r = XX, p = YY".

# Correlation test between cd40 and days

cd4_cor <- cor.test(formula = ~ cd40 + days,
                    data = ACTG175)

cd4_cor_r <- cd4_cor$estimate  # Get the correlation
cd4_cor_p <- cd4_cor$p.value   # Get the p-value

C2. In addition to the correlation test, include a relevant scatterplot showing the relationship between CDT4 T cell count at baseline and number of days until a major negative event.



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