This week will shift gears and talk about writing reports using R Markdown.
R Markdown is a format that allows you to combine text and R code.
From RStudio you can output your file in many formats: html, PDF, Word,
presentations. We are exploring a small slice of R Markdown.
If you have never worked with R Markdown, start with RStudio’s online
lessons. That’ll get you up to speed with the basics. RStudio R Markdown
lessions
When you are ready to explore more, go to this great R Markdown resource R Markdown for Scientists. The R Markdown Definitive Guide is also a good resource.
Quarto is also created by RStudio and is the “next-gen” R Markdown. If you are just starting out, just stick with R Markdown. Quarto is back compatible with R Markdown. But going forward, everyone will be switching to Quarto. I’ve started converting my easy websites and simple online documentation to Quarto. It’s not hard. Note the material here will work for Quarto.
There are many on-line tutorials for R Markdown which will cover the basics. I’ll focus on some of the aspects of R Markdown related to creating the types of reports that we generate in NOAA Fisheries (and other agencies). These often are reports that have a standard format but use different data sets or that generate the same table or figure with different data (species, gear, location, etc). Automating the generation of these reports can really speed up your work:
Part 1 today
Part 2 next week
Today I will use RStudio Cloud so you can follow along without having to install any packages or TeX (for PDF generation).
Click here and sign in with a Google account (any one you want) to follow along. You can also make an account.
If you want to follow along on your computer, then create a RStudio project using this repo https://github.com/RVerse-Tutorials/Rmarkdown-Tutorial.git
Note If you are using RStudio on your own computer, there are some packages that you’ll need: {rmarkdown}, {knitr}, {kableExtra}, {flextable}, {ggplot2}, {xtable}.
To make PDF files you will need a LaTeX installation. If you don’t have one already (if you are not sure, then you don’t), you can install tinytex. Run these lines:
install.packages('tinytex')
tinytex::install_tinytex()
Note the first time you make a PDF, {tinytex} will load any needed packages and it can take a long time. You’ll see a spinning wheel on the R Markdown tab.
Note, if you are using RStudio Cloud, I have installed all these things for you.
Open up Basic.Rmd
.
At the top you see
---
title: "Basic"
author: "EE Holmes"
output: html_document
---
This is the yaml
which gives the instructions about how
to process your R Markdown file. The yaml
file is sensitive
to spaces. Do not add or subtract spaces. This yaml
is very
simple, but the yaml
can be complex for some applications.
We will only be working with simple examples.
After the yaml
is the content. To see a summary of R
Markdown formating, click ‘Help’ menu and then ‘Markdown Quick
Reference’.
We can create documents in different formats from this Rmd file. Click the drop-down menu next to the ‘Knit’ button.
Try creating an Rmd file File > New File > R Markdown… and see the templates that are offered.
In RStudio, click the ‘Help’ tab in the top nav bar. You will see ‘Markdown Quick Reference’. That has basic markdown syntax. You can also click ‘Cheatsheets’ and there are two references sheets. Using Google, you can also find answers to any questions that the RStudio help files doesn’t answer. See also R Markdown for Scientists.
Tips:
Figures.Rmd
Figures_for_loop.Rmd
Table.Rmd
Table_Extras.Rmd
Tables_for_loop.Rmd
Math.Rmd
Extras.Rmd
Here are some real examples of NOAA Fisheries reproducible reports created with R Markdown.
Northeast US State of the Ecosystem reports, and technical documentation: Website with links to reports, GitHub repo for Mid Atlantic report ❖ GitHub repo for New England report ❖ GitHub tech doc repo ❖ K Bastille paper on the report and process ❖ K Bastille presentation
Atlantic Offshore Wind Development Socioeconomic Reports These reports are all done in R Markdown. You can see how they use a standard format.
{sa4ss} R package for creating stock assessment documents for the Pacific Fishery Management Council. The package provides (1) a consistent structure, (2) generic text that should be the same across all stocks, (3) embedded functionality to create an accessible pdf that satisfies NOAA’s guidance for 508 compliance, and (4) increased speed compared to creating a word document from scratch.
{NMFSReports} R package The NMFSReports Package has all of the basic architecture you need to create reproducible and repeatable NOAA Tech Memos in R Markdown! This approach is perfect for efficiently rolling out annual (or other regular) reports or reports with formulaic sections (the same chapter structure but for a different area or species). Scripts integrate table, figure, data, and bibliography management and design automation.
Fisheries Economics of the United States The 2016-2019 reports were produced with R Markdown. This shows you that you can customize the output of your reports to achieve a specific, and uniform, formating.
Main.Rmd
is re-knit. So if the data change, all the
analyses will be updated.README
file.Automatic figures and table numbering for a paper or a report is a hassle with R Markdown. Here is my solution. The Journal Article repo uses this method. 2022: last I checked people were still using this method, but I suspect that this will be solved in the future.
Knitting in RStudio by clicking the Knit button uses
rmarkdown::render()
which will automatically produce an
html document that is more accessible than other ways that you might
knit a R Markdown document. However you’ll need to do some special
things to add alt info to figure in the html, specifically add a figure
caption.
If you don’t need to have figure captions or if you are ok with the figure legend and alt text being the same, they adding alt text for figures is quite easy.
Figure caption displayed and alt text will be the same
plot(1:10)
No figure caption displayed
Add this to the top of your Rmd (or save to a css file and put that in the yaml) and the figure caption will not show up in your html.
<style>
.caption {
display:none;
}
</style>
Use html
Another approach is to have R Markdown save your figures, and then insert those with html. Then you have full control over the caption, alt text and can add the longdesc tag also.
NMFS R UG session on 508 Compliance and R Markdown Take a look at the {sa4ss} R package which has accessibility functionality added.
https://r-resources.massey.ac.nz/rmarkdown/
Latex (PDF)*
Making accessible PDFs is harder in LaTeX. You might look at the tagpdf.
Another option is to have R Markdown save the figures, and use LaTeX:
\begin{figure}
\centering
\includegraphics{fig1.png}
\Description[short desc]{long description}
\caption{the caption}
\label{fig:fig1}
\end{figure}