Chapter 8 Generating report with R
Hopefully through this workshop, you have been editing your R code in a knitR (R markdown) document, and now have a record of all the analyses that you have carried out over the course of the workshop. The final exercise is to tidy up these documents to produce the final report.
Typically at QFAB, we might produce two R markdown documents for an analysis. The first is a template which contains all the R code, commentry on what the code is doing, section headers and so on, while the second is build around this template with data-specific interpretive comments inserted where relevant.
The first document allows us to reuse the template and re-run the same analysis pipeline on a comparable input sample at a later date, while the second forms the final analysis report for a specific sample (the comments have to be inserted after the analysis of course, because the same analysis process on two different samples may well give two very different outcomes).
-
Working with one (or more) of your knitR documents, tidy up the code and documentation to produce a reusable template format of that dataset.
-
Create a copy of the template and add some interpretation and commentary to generate a final, reproducible report
-
Hints:
- Use the RStudio File > Save As function to create a copy of the template for the report
-
Give the chunks names to make their purpose easier to remember (to do this, use the format
{r chunk_name}
, instead of just{r}
at the start of the chunk)
-
Hints:
You can hide R code in your report but still include the output from that code by adding echo = FALSE
to the chunk header (e.g. {r chunk_name, echo = FALSE}
). If you want to run R code without showing either the code or the output, use include = FALSE
- See http://kbroman.org/knitr knutshell/pages/Rmarkdown.html for descriptions of some of the other chunk display options
Once you have generated your final knitR report, click on the Save As link at the top of the preview window, and you can download the report to your local computer in HTML format. When using the web-based version of RStudio, this is the only option to save knitR output; however, if you install and run R and RStudio locally, you can also generate PDF and Word format reports.