Reproducible Reports in R
0.1 Exercise 1
Create a project in Rstudio associated with this course/book using the directions at this link.
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.
Copy (don’t cut and paste)
Exercise0.1.Rmd
from theCodeTemplates
subdirectory of theStats4EcologistsFiles
GitHub repository contained here to the directory you created in step 1.Use the
here
package (Müller, 2020) and code in the code template to read in theminnesota-history.csv
data set in theFW8051Data
subfolder. This data set contains COVID-19 tracking data from Minnesota. I downloaded these data from this site. You can find metadata here.
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:
- 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:
- https://www.statmethods.net/input/dates.html
- https://r4ds.had.co.nz/dates-and-times.html
- your colleagues in this class!
- 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 thermarkdown
package by typing the following into the R console (after changingRcode
andfilename
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.
- Follow steps 1-4 of
Exercise 0.1
. - 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.
- 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
- 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:
- RStudio’s tutorial: https://rmarkdown.rstudio.com
- The cheat sheet: https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
- The knitR book: https://yihui.name/knitr/
- The R Markdown cookbookhttps://bookdown.org/yihui/rmarkdown-cookbook/