Chapter 10 Creating an R Markdown report

In an automated workflow like this one it can be helpful to have all of our important analytical outputs compiled in one place and in a format suited to easy review. In this example we’ll do this using an automatically populated R Markdown report.

The R Markdown report we’re going to use is "example_project/R/analysis_report.Rmd". If you open the file up you should see something like this:

---
title: "Analysis report"
output: html_document
---

So far it’s very simple. The top few lines are a YAML header that currently defines the title of the document as “Analysis report” and specifies that an html document should be produced when the file is rendered. Some options for editing this header are discussed here (HTML files) and here (PDF files).

This document is a great place to explain the findings of your research project and there is a lot that you can do to personalize the content it displays. We include minimal narrative writing here just to get the point across:

---
title: "Analysis report"
output: html_document
---

``{r echo=FALSE}
tar_load(exploratory_plot)
tar_load(penguin_model)
```

This document contains the results of our analysis of the relationship between flipper length and body mass in Adelie, Gentoo, and Chinstrap penguins. 

### Flipper length and body mass plotted by species
``{r}
exploratory_plot
```

### Outputs of models testing for a statistical relationship between flipper length, body mass, and species
``{r}
summary(penguin_model)

coef(penguin_model)
```

The first code chunk uses tar_load() to load specific targets into the environment. A similar method would be tar_read(), which reads in the target’s value but doesn’t permanently load it into the environment without assignment like model <- tar_read(penguin_model). With the targets loaded we are then able to print and manipulate them as normal. A model summary and coefficient table are included in the document.