Chapter 14 Report-template
Here are a few suggestions to help in the writing of a “report” for the analysis of your NHANES chemical data that can be created as an R
markdown document containing all of the report narrative (story,) analysis code (shown or hidden,) graphs and illustrations.
A single file with most of the code from this chapter as a “template” can be found online as a plain text file but with either .Rmd
or .Rmd.txt
filename extensions. The content is identical in both files. Depending on the settings of your brownser most likely the .txt
version will appear within the browser. For just .Rmd
it may appear within the browser of be downloaded automatically in your default Downloads
folder.
Sample report template in R
markdown for download:
14.1 Overall template format
The report should be in the R
markdown format with a YAML header and a body with markdown and R
code. A minimal outline could be:
---
title: "NHANES report"
author: "your name"
output: html_document
---
# Preface {-}
Some background on something if wanted. Or remove
# Introduction
Some useful into.
# Chemical info
The chemical studied and why
# NHANES data
What is it, where to find
## Download
In this section the download code an optionally be hidden
## Selected data
What are the columns of data to be used.
# Analysis
Some kind of analysis. With R code shown or not.
May include tables, graphics etc.
# Results
This may be a summary of some of the analysis
# Conclusion
Is there a general conclusion that can be drawn from the analysis and the results?
14.2 YAML example
This part may be simple or more complex, for example requesting figure caption or requesting the automatic creation of a table of contents with a specified number of levels. For HTML the table can be “floating” as shown on the left hand side for easier navigation.
Here is an example YAML for all 3 major output formats with these options. The line fontsize: 12pt
is most useful for PDF output avoiding the 10pt
default.
---
title: "NHANES Report Example"
author: "Your Name Here"
date: " 07 July, 2022 "
output:
word_document:
toc: true
toc_depth: 2
fig_caption: true
pdf_document:
toc: true
toc_depth: 2
fig_caption: true
number_sections: true
html_document:
toc: true
toc_depth: 2
toc_float: true
fig_caption: true
number_sections: true
fontsize: 12pt
---
14.3 General chunk options
The default R
markdown template in RStudio automatically adds this general option chunk that can be expanded. For example, to make all R
code hidden change echo = TRUE
to FALSE
. The code in these options apply to all code in the document but can be overridden by placing the opposite or desired option within the individual {r}
tags in each chunk.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
14.4 Preamble, Preface and Introduction
“Preamble” or a “Preface” are often optional but usually not numbered as the rest of the sections. To prevent numbering we add {-}
so that the numbering would start at the next heading tag #
for example for # Introduction
.
14.5 Activating packages
It may be useful to activate some packages at the beginning to make sure they are “up” for later. Hiding the code is useful for a generic report. The default RStudio options of echo=FALSE
and warning=FALSE
are not enough to suppress all messages. The following code should load tidyverse
quietly:
```{r eval=TRUE, echo=FALSE, warning=FALSE}
# Load quietly here and add a code below with eval=FALSE
options(tidyverse.quiet = TRUE)
library(tidyverse)
```
14.6 Live web links
NHANES or other web references can be created as “live” links in all document types by using the format [Name in Square brackets](http://web.site.address.here)
For example: [NHANES web site](https://www.cdc.gov/nchs/nhanes/)
.
14.7 Embedding graphs
Graphs can be embedded with optional legends. The age distribution histogram without showing R
code. Since age ranges from 0 to 80 there are 81 “slots” all represented individually by specifying breaks = 81
. Alignment can be specified. Optionally width and height are added and expressed in inches by adding fig.width=7, fig.height=5
.
```{r echo = FALSE, fig.cap="Histogram of age distribution", fig.align='center'}
with(nhanesDemo , hist(RIDAGEYR, breaks = 81))
```
14.8 Inline code
Inline code is the secret that can help make your report precise and useful as it allows you to access and print information in the report that you do not have to know and most of all that is not necessary to copy/paste.
Inline code can be fancy and contain more than just a simple computation such as `r 1+1`
. Indeed it can even be a pileline as shown in this example:
There are `r dim(nhanesDemo)[1]`
observation for
`r select(nhanesDemo, RIDAGEYR) %>% filter(RIDAGEYR < 18 ) %>% count()`
children participants less than 18, `r select(nhanesDemo, RIDAGEYR) %>% filter(RIDAGEYR > 18 & RIDAGEYR < 80) %>% count()`
adult participants between 18 and 79 and
`r select(nhanesDemo, RIDAGEYR) %>% filter(RIDAGEYR >= 80) %>% count()`
adults over the age of 80.
This will be rendered in the final text as:
There are 9971 observation for 3979 children participants less than 18, 5478 adult participants between 18 and 79 and 376 adults over the age of 80.
14.9 Math formula
Examples of math formulas can be found at:
One $
sign keep the formula in line. Two $$
make the formula displayed on a different line. For example:
The creatinine adjustment requires a division and a multiplication by \(10^-4\). The final formula is \[ratio= \frac{Analyte}{Creatinine}*10^{-4}\]
14.10 Addendum
In a technical report is it customary to also report how your R
session was at the moment of computation. This is accomplished by adding the command sessionInfo()
In this example the eval=FALSE
makes that the code is not run. Update as needed depending on the type of report that you write and who it is for.
sessionInfo()