---
title: "NHANES Report Example"
author: "Your Name Here"
date: " `r format(Sys.time(), '%d %B, %Y')` "
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
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Preamble {-}

This is a report on NHANES data. (This section is *not* numbered by using `{-}`) 

*NOTE*: for MSWORD output `number_sections: true` causes an error. this is because numbering should be done within Word itself, or call a template that contains the numbering option. See details in https://bookdown.org/yihui/rmarkdown/word-document.html

This section can serve as "Preface" or "Preamble" for example.

The line `fontsize: 12pt` is mostly relevant for PDF output that has a default of 10 and prints very small.

# 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: (testing this template will show that nothing is printed here!)


```{r eval=TRUE, echo=FALSE, warning=FALSE}
# Load quietly here and add a code below with eval=FALSE
# for the demo1
options(tidyverse.quiet = TRUE)
library(tidyverse)
```

# Introduction

The chemical **X** has been studied extensively in the last two decades and ...

# Description of NHANES data

The National Health and Nutrition Examination Survey (NHANES) data for years 2015-2016 are used in this report and can be downloaded from the [NHANES web site](https://www.cdc.gov/nchs/nhanes/) or directly from the [2015-2016 data page](https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=2015).

The relevant documentation files are files are for datasets ....*e.g.* [DEMO_I](https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.htm), [PFAS_I](https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/PFAS_I.htm),
[ALB_CR_I](https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/ALB_CR_I.htm) etc.
and the corresponding data files. These are in the "export" format as created by the `SAS` software and can be downloaded ...

## Chosen variables

This could be a place to specify the variables that are going to be used EVEN if the "Master" will contain all columns.

*NOTE*: *The files can be downloaded within the report, WITHOUT showing the code if that is not relevant or desired by using `echo = FALSE` in the `` {r} `` call. This allows everything to be contained within this document and is a step forward for record keeping and reproducible research.*


```{r echo=FALSE}
# Write the download code here
# Use either the foreign or haven package
# not forgetting the library() call
library(haven)
nhanesDemo <- read_xpt(url(
"https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT"))
# etc.
```


Describing the new object with "inline code": The "demographic data contains `r dim(nhanesDemo)[1]` records and `r dim(nhanesDemo)[2]` variables.

# Making a graph

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', fig.width=7, fig.height=5}
with(nhanesDemo , hist(RIDAGEYR, breaks = 81))
```

Using **inline code** we can extract information to fill-in values **without copy** paste simply by **querying** the data. The can avoid a lot of erroneous reporting.

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.

# Math formula

One `$` sign keep the formula in line. Two `$$` make the formula displayed on a different line.

The creatinine adjustment requires a division and a multiplication by $10^-4$ The final formula is $$ratio= \frac{Analyte}{Creatinine}*10^{-4}$$

examples of formulas can be found at:  
* https://www.calvin.edu/~rpruim/courses/s341/S17/from-class/MathinRmd.html   
* and http://www.math.mcgill.ca/yyang/regression/RMarkdown/example.html


# Conclusion

In this report we learn that chemical **X** is ... something and something else.

# 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.

```{r eval=FALSE}
sessionInfo() 
```


