Reproducible Reports in R

0.1 Exercise 1

  1. Create a project in Rstudio associated with this course/book using the directions at this link.

  2. Create a subdirectory to hold your scripts as you work through the book. You may label this anything you want, but I would suggest avoiding spaces or special characters in the name.

  3. Copy (don’t cut and paste) Exercise0.1.Rmd from the CodeTemplates subdirectory of the Stats4EcologistsFiles GitHub repository contained here to the directory you created in step 1.

  4. Use the here package (Müller, 2020) and code in the code template to read in the minnesota-history.csv data set in the FW8051Data subfolder. This data set contains COVID-19 tracking data from Minnesota. I downloaded these data from this site. You can find metadata here.

library(here)
coviddat <- read.csv(here("Data", "minnesota-history.csv"))

Using the here package (or, alternatively, a relative path), will make it easy to someday transfer your code to another computer or to share (say with a friend) and still have the code work right out of the box. This will not likely be the case if you use something like:

coviddat <- read.csv("c:\\My Documents\\MyStatsClass\\minnesota-history.csv")
  1. Add R code necessary to plot the number of deaths over time. This will require that you grapple with a date-time variable in R. Some resources that may help if you struggle to figure out how to work with dates in R include:
  1. Create a reproducible pdf (or html) document with code and output. This can be accomplished by clicking on the “knit” button in Rstudio. Alternatively, you might want to save the document to an Output directory (to keep things more tidy). This can be accomplished using the rmarkdown package by typing the following into the R console (after changing Rcode and filename to match the correct directory and file name on your computer):

rmarkdown::render(here::here("Rcode", "filename"), output_dir = "Output", output_format="pdf_document")

0.2 Exercise 2

For this exercise, we will create a reproducible document using the same steps as in Exercise 0.1. However, we will work with an R script rather than a .Rmd file.

  1. Follow steps 1-4 of Exercise 0.1.
  2. Create an R script from your .Rmd file from exercise 0.1 using the following code:

knitr::purl(here::here("CodeTemplates", "Exercise0.1.Rmd"), documentation = 2)

This will create a .R file in your main project directory.

  1. Copy this .R file into the code directory you created in step 2 of Exercise 0.1 and open the file. Note that:
  • the YAML header and any text from your .Rmd file is included with a #' in front of it.
  • any code chunk options will be included with a #+.

You can then create your reproducible report by clicking File > Compile Report (and then selecting pdf, html, or MS Word). Alternatively, you can use the rmarkdown package as described in Exercise 0.1.

0.3 Exercise 3

  1. Create a report containing the following:
  • Bold and italicized text
  • The symbol: \(\beta\)
  • The equation: \(\pi r^2 = 12\)
  • A link to a website
  • Some simple R code and a figure

If you get stuck, consult the following resources:

References

Müller, K. (2020). Here: A simpler way to find your files. Retrieved from https://CRAN.R-project.org/package=here