Generating reports from R

What are you going to do?

  • Read and write well-documented and well-structured code
  • Use this to generate clear and well-documented results/reports

What are you going to do?

  • Read and write well-documented and well-structured code
  • Use this to generate clear and well-documented results/reports

Header information

#' ---
#' title: "Comparing a simple growth model with a birth-death model"
#' author: "Richard Reeve"
#' date: '`r format(Sys.Date(), "%B %d %Y")`'
#' output: html_document
#' ---
#'

Mixed text and code

#'
#' Load in the functions that do the work
library(RPiR)
source("0104-step-growth.R")
source("0105-step-birth-death.R")

Formatted text – lists and maths

#' We are going to compare two population dynamics models:
#'
#' 1. A simple growth model
#'    $$N(t + 1) = \lambda \times N(t) + N(t)$$
#' 2. A birth death model
#'    $$N(t + 1) = b \times N(t) - d \times N(t) + N(t)$$
#'

(note the blank #’ line before the numbered list)

Descriptive text vs code comments

#' First we set up the simulation parameters for every experiment.

## Set the birth and death rates
birth.rate <- 0.2
death.rate <- 0.1

## Starting population size
initial.count <- 1

Formatted text – headers

#'
#' ## Run the full $birth, death$ simulation

## Set up the population starting size (at the first timestep)
population.df1 <- data.frame(count = initial.count)

(note the blank #’ line again before the section header)

Plotting and chunk options

#+ initial, fig.width=5, fig.height=5, fig.align="center", fig.cap="Fig. 1: Birth-death model"
plot_populations(population.df1)
Fig. 1: Birth-death model

Fig. 1: Birth-death model

More chunk options for output

You can make it obvious what the R output is by default:

a <- 5 + 3
a
## [1] 8

Or you can remove the (slightly odd) comments on output:

#+ comment=""
b <- 4 + 4
b
[1] 8

Or you can run code directly inside your text by using:

#' What is b - a? `r b - a`.

What is b - a? 0.

Further details