This is an example R Markdown document

@Book{rmarkdown-cookbook,
  title = {R Markdown Cookbook},
  author = {Yihui Xie and Christophe Dervieux and Emily Riederer},
  publisher = {Chapman and Hall/CRC},
  address = {Boca Raton, Florida},
  year = {2020},
  isbn = {9780367563837},
  url = {https://bookdown.org/yihui/rmarkdown-cookbook},
}

@Manual{rmarkdown-package,
  title = {rmarkdown: Dynamic Documents for R},
  author = {JJ Allaire and Yihui Xie and Christophe Dervieux and Jonathan McPherson and Javier Luraschi and Kevin Ushey and Aron Atkins and Hadley Wickham and Joe Cheng and Winston Chang and Richard Iannone},
  year = {2024},
  note = {R package version 2.27},
  url = {https://github.com/rstudio/rmarkdown},
}

Including R code in your Markdown document

You have to put all of your code in a “Code chunk” and tell knitr that you are using R code.

meas <- readRDS(here::here("data", "measles_final.Rds"))
str(meas)
## 'data.frame':    12438 obs. of  7 variables:
##  $ iso3c           : chr  "AFG" "AFG" "AFG" "AFG" ...
##  $ time            : int  1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 ...
##  $ country         : chr  "Afghanistan" "Afghanistan" "Afghanistan" "Afghanistan" ...
##  $ Cases           : int  2792 5166 2900 640 353 2012 1511 638 1154 492 ...
##  $ vaccine_antigen : chr  "MCV1" "MCV1" "MCV1" "MCV1" ...
##  $ vaccine_coverage: int  11 NA 8 9 14 14 14 31 34 22 ...
##  $ total_pop       : chr  "12486631" "11155195" "10088289" "9951449" ...

You can make plots and add captions in Markdown as well.

meas_plot <- subset(meas, country == "India" & vaccine_antigen == "MCV1")
plot(
    meas_plot$time, meas_plot$Cases,
    xlab = "Year",
    ylab = "Measles cases by year in India",
    type = "b"
)
Meases cases over time in India.

Meases cases over time in India.

Note that if you want to automatically reference your figures like you would need to for a research paper, you will also need to use the bookdown package, and you can read about it here. For this document, we would have to write out “Figure 1.” manually in our text.

Including tables and figures from files

Including tables is a bit more complicated, because unlike plot(), R cannot produce any tables on its own. Instead we need to use another package. The easiest option is to use the knitr package which has a function called knitr::kable() that can make a table for us, like this.

meas_table <- data.frame(
    "Median cases" = median(meas_plot$Cases),
    "IQR cases" = IQR(meas_plot$Cases)
)

knitr::kable(
    meas_table,
    caption = "Median and IQR number of measles cases across all years in India."
)
Median and IQR number of measles cases across all years in India.
Median.cases IQR.cases
47072 44015.5

You can also use the kableExtra package to format your table more nicely. In general there are a lot of nice table making packages in R, like we saw with the tinytable package in the exercise.

tinytable::tt(meas_table)
tinytable_o73g4xhg2p32dbgu79cj
Median.cases IQR.cases
47072 44015.5

Finally, if you want to include a figure that you already saved somewhere, you can do that with knitr also.

knitr::include_graphics(here::here("images", "xkcd.png"))

R Markdown resources

References

Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.