13 R Markdown for literate programming and automated reports

This discussion thread is for gathering knowledgebase related to R Markdown: https://rmarkdown.rstudio.com/

It can be used to generate reports, slides, websites, dashboards, shiny app, books, emails. It is the tools allows you to do literate programming, which – as defined by Donald Knuth - is the type of programming where human language is combined with computer language, making the code much easier understood by your colleagues and yourself and coding much more fun.

13.2 Automated generation of multiple PDF files

Question: “Can we automatically generate multiple PDF files using R RMarkdown? E.g. 20 report cards in PDF format, each showing results for 50 different parameters of the analysis?”

Answer: “Absolutely!” - This is what RMarkdown is developed for.

Here’s a tiny R script example showing how to do it.

# report_generator.R

mvndr_nm <- c('name1', 'name2', 'name3', 'name4', 'name5', 'name6')
mvndr_nbr<- c('60031167', '60688509', '60074051', '60148060', '60086898', '60080204')

for (i in seq_along(mvndr_nm)) {
  rmarkdown::render("C:/Users/santi/Documents/R Scripts/Export_Data_CSV.Rmd",
                    output_file = sprintf("MVNDR_%s.html", mvndr_nbr[i]),
                    params = list(MVNDR_NBR = mvndr_nbr[i], MVNDR_NM = mvndr_nm[i]))

# Export_Data_CSV.Rmd
# (remove # and REMOVETHIS_ and replace \' with \` below)

# - - -
# output:  html_document:
# REMOVETHIS_params:
# - - -

This document  provides a summary of "' r REMOVETHIS_params$MVNDR_NM'" performance within in QC

Source: - https://stackoverflow.com/questions/67739377/passing-multiple-parameters-in-rmarkdown-document - https://stackoverflow.com/questions/60203785/using-multiple-params-in-rmarkdown-yaml-fields

13.3 Useful tricks and tips

13.3.1 In RStudio editor

In Tools -> Global Options… -> R Markdown

  • Choose "Show in Document outline: BOTH Sections and Chunks.
  • Uncheck “Show output inline”, so you can see it Console as you normally do when running from .R file

In Tools ->Global Options… -> Pane Layout and Appearance, - you can add additional Pan (starting from RStudio 1.2) - We like it having Console window on left (to see more!) - Some also like dark theme (eg. “Vibrant ink” diplays comments well, others do not)

13.3.2 Spliting Rmd in chunks

  • Split your long Rmd into small ones!
  • Give them names - sorted / tagged by order and keywords
  • For debugging or simplify your work use knitr::knit_exit() r code.
  • Use (PART) and {-} when you need to split in part and not to index the page

See index.Rmd of this book. Below is excerpt from them

  # (PART) Community Codes {-}
  ` ` `{r 21_gc_datasets, child = '21_gc_datasets.Rmd'}
  ` ` `

  # (APPENDIX) Appendices {-}
  ` ` `{r 40_ca_packages, child = '40_ca_packages.Rmd'}
  ` ` `

  ` ` `{r include=FALSE}
  knitr::knit_exit() # Stop compiling here
  ` ` `
  ` ` `{r 99_some_notready_parts, child = '99_some_notready_parts.Rmd'}
  ` ` `

13.3.3 Making good use of configuration yaml header in index.Rmd file

See index.Rmd of this book and the other (forked) books in https://github.com/open-canada/

13.3.4 Conditional execution of chunks From smltar book

Excerpt from https://github.com/open-canada/smltar/blob/master/index.Rmd