Trulli
www.rstudio.com

Overview

By the end of this practical you will know how to:

  1. Create a new R Markdown file (.Rmd)
  2. “Knit” an R Markdown file to an .html or .pdf report
  3. Format text using formatting tags
  4. Create code chunks
  5. Add plots and tables
  6. Include code output in sentences with inline chunks

Datasets

File Description Rows Columns
mcdonalds.csv Nutrition information from McDonalds menu items 260 24
happiness.csv Results from the World Health Organisation happiness survey 155 12

Packages

Package Installation
tidyverse install.packages("tidyverse")
knitr install.packages("knitr")
DT install.packages("DT")
broom install.packages("broom")
rmdformats install.packages("rmdformats")

Cheatsheet

Tasks

A - Setup

  1. Open your baselrbootcamp R project. It should already have the folders 1_Data and 2_Code. Make sure that the data files listed in the Datasets section above are in your 2_Data folder

  2. Because R Markdown looks quite a bit different from standard R code, the best way to look at examples is to see a new R Markdown document in action. In RStudio, click File – New File – R Markdown

  1. Give the document a title and an author. For the output format, select HTML (the default). Click Ok!

  1. A new file that looks like this should open up. This is your first R Markdown document!

  1. Save your markdown file in your main project directory (not a sub-folder – you’ll put it there later!) under the name dynamicreports_practical.Rmd

  2. Now knit your document to an HTML file. To do this, click the knit button (or use the Command + Shift + K shortcut)

  1. Now you should see your final, HTML document! Scroll up and down the document and see how she looks!

B - Here’s your goal!

  1. Your goal in this practical is to create as much of this html document as you can! Open the document and keep it open in a separate window as you work through the practical.

C - Load packages, data and set default chunk options

  1. The first chunk in your document already has the label setup. This is your setup chunk. This is a good place to load your packages and import your data. Inside of this chunk, write the comment # Load Packages -------------. Then, using the library() function, load the packages listed in the Packages section above.

  1. Knit your document! Make sure you don’t get any errors!

  2. Now it’s time to load your datafiles. In the same setup chunk, load each of the data files listed in the Datasets section.

# --- Load packages
library(XX)
library(XX)

# --- Load data
XXX <- read_csv()
XXX <- read_csv()
  1. Knit your document! Make sure you don’t get any errors!

  2. Now it’s time to change some of the default chunk options. Inside of your setup code chunk change the existing values of knitr::opts_chunk$set to the following:

# INCLUDE ALL OF THIS CODE IN YOUR FIRST 'setup' CHUNK!

knitr::opts_chunk$set(fig.width = 6,        # Figure width (in)
                      fig.height = 4,       # Figure height (in)
                      echo = FALSE,          # 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. Knit your document! Make sure you see an output!

D - Add formatted text and an image

  1. Below your last markdown chunk, write the necessary text and Markdown tags to create the following sentence. Be sure to create italic text using single astricies *ITALIC* and and bold text using two astricies **BOLD**
  1. Create a new third level header with the text “Overview” using three hashtags ### Overview

  2. Knit the document!

  3. Create a new code chunk by either clicking the “Insert - R” button, or by using the “Command + Option + I”" shortcut on Mac

  4. Now it’s time to add an image to your document! In the chunk you just created, use the following template to include an image of a burger in your document. You can find the image here: https://upload.wikimedia.org/wikipedia/commons/thumb/4/4d/Cheeseburger.jpg/2560px-Cheeseburger.jpg.

# Include a buger image

include_graphics(path = "XXX")
  1. Knit the document!

  2. Was your image too big? To make the image a bit smaller, include the chunk argument out.width = "30%".

  3. Knit the document!

  4. Include the appropriate text and formatting to write the sentence below. When writing the number of columns and rows, use in-line chunks to get the data directly from the mcdonalds object! You can get the number of columns with ncol(mcdonalds) and the number of rows with nrow(mcdonalds)

  1. Knit the document!

E - Add a formatted table with datatable()

  1. Add a new 3rd level header called “Data”

  2. Below the header, write the sentence: “Here is a table showing the first 6 columns in the data”

  3. Create a new code chunk

  4. Inside the chunk, include the following code which uses the datatable function to render a dataframe as a nicely formatted HTML table.

# Print the first 6 columns of the data as a datatable
mcdonalds %>% 
  select(1:6) %>% 
  datatable()
  1. Knit the document! Do you see your nicely formatted table?

  2. Play around with your table. Instead of selecting the first 6 columns, select the columns Item, Category, Calories, Sodium

  3. Knit the document!

F - Add plots

  1. Add a new 3rd level header called “Calories Histogram”

  2. Write the appropriate combination of markdown, text, and code to create the following output. Be sure to specify the mean and maximum umber of calories using inline chunks!. Here’s some ggplot2 code that might help in creating your plot.

# Create a histogram of Calories

ggplot(data = mcdonalds,
       aes(x = XX)) +
  geom_histogram(col = "white") +
  labs(title = "XX",
       subtitle = "XX",
       caption = "XX") +
  theme_bw()
  1. Knit the document! Diagnose and correct any errors!

  2. Add a new 3rd level header called “Calories and Sodium”

  3. Write the appropriate combination of markdown, text, and code to create the following output (Here’s some ggplot2 code that might help!):

# Create a scatterplot showing the relationship
#  between Calories and Sodium

ggplot(data = XX,
       aes(x = XX, y = XX)) +
  geom_point() +
  labs(title = "XX",
       subtitle = "XX",
       caption = "XX") +
  theme_bw()
  1. Knit the document! Diagnose and correct any errors!

  2. Add a 3rd level header called “Calories by Category”

  3. Write the appropriate combination of markdown, text, and code to create the following output (Here’s some ggplot2 and dplyr code that might help!)

# ggplot

ggplot(data = XX,
       aes(x = XX, y = XX, fill = XX)) +
  stat_summary(geom = "bar", fun.y = "mean") +
  guides(fill = FALSE) +
  labs(title = "McDonalds Menu Items",
       subtitle = "Created with ggplot2",
       caption = "Source: Kaggle.com") +
  theme_bw()

# dplyr

Calories_agg <- XX %>%
  group_by(XX) %>%
  summarise(
    Min = min(XX),
    Mean = mean(XX),
    Median = median(XX),
    Max = max(XX)
  )

kable(x = Calories_agg, 
      caption = "Summary Statistics of McDonalds Menu item Calories")

G - Add a table of statistical test results

Now let’s show the results of a regression analysis predicting the sodium content of an item as a function of its calories.

  1. Add a new 3rd level header called “Regression”

  2. Using the appropriate combination of text and code chunks, create the following output. Use the following template for help!

# Create regression model predicting Sodium from Calories

cal_sod_lm <- lm(formula = XX ~ XX,
                 data = mcdonalds)

# Export cal_sod_lm to a nicely formatted table

kable(x = tidy(cal_sod_lm), 
      digits = 3, 
      caption = "Regression analysis predicting Sodium from Calories")

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

I - Add tabs!

  1. Now it’s time to convert some of your sections to tabs! To do this, just add the second level header ## {.tabset} above your ### Overview header

  2. Knit your document! Do you see the tabs now?

X - Add results from the happiness survey

  1. If you’ve gotten this far, then you’ve really gotten the hang of R Markdown! Try adding the correct combination of text, tags, and code to add all of the results from the happiness data contained here: https://therbootcamp.github.io/BaselRBootcamp_2018July/_sessions/DynamicReports/DynamicReports_answers.html