Skip to content

Instantly share code, notes, and snippets.

@davebraze
Last active January 7, 2022 19:56
Show Gist options
  • Save davebraze/b615bd8f85f9a9dbe0a5f46e797872c3 to your computer and use it in GitHub Desktop.
Save davebraze/b615bd8f85f9a9dbe0a5f46e797872c3 to your computer and use it in GitHub Desktop.
Rmarkdown Notes

Knitr options

These are the global knitr options I use for most documents. They get set in the very first code chunk of each doc. The out_type bit is there to tailor the output device to the particular type of document being created.


out_type <- "other"
if (knitr::is_html_output()) {
    out_type <- "html"
} else if (knitr::is_latex_output()) {
    out_type <- "pdf"
}

knitr::opts_chunk$set(fig.width=7,
                      fig.height=7*9/16,
                      fig.align = "center", ## horz center figs on page
                      out.width="95%",
                      out.height="95%", 
                      dpi=if(out_type=="pdf") 300 else 72,
                      dev=if(out_type=="pdf") "cairo_pdf" else "png", 
                      dev.args=if(out_type=="pdf") NULL else list(type="cairo-png"),
                      eval=TRUE,
                      echo=FALSE,
                      include=FALSE,  ## set TRUE chunk-wise for those whose output should be included
                      results='asis',
                      purl=TRUE,
                      cache=FALSE,
                      warning=FALSE,
                      message=FALSE,
                      knitr.kable.NA = "--"
                      )

Special Characters

When using an Rmarkdown to pdf workflow, it is necessary to escape some characters that have special meaning to latex in order to get them to print correctly in the final document.

  • The one that has bitten me more than once is the underscore ("_"). In the plain text portion of an rmarkdown file, it is sufficient to prefix each underscore with a backslash. So, instead of variable_name, escape the underscore with a backslash like this: variable\_name. In the context of a table or figure caption, the backslash must be doubled (variable\\_name); ampersand & must also be escaped within a caption \\&.

Figures and Tables

Useful latex options

If making PDF files, you can put a header-includes block in your YAML section to enable and configure various latex packages. For details of how to configure each latex package, see its documentation.

header-includes:
  - \renewcommand{\abstractname}{Executive Summary}
  - \usepackage{draftwatermark} # add a "Draft" watermark to each page.
  - \usepackage{pdflscape} # use to rotate specific pages to landscape orientation
  - \usepackage{booktabs}
  - \usepackage{caption} # may be especially important when also using "longtable"
  - \usepackage{longtable}
  - \usepackage[labelfont={bf}]{caption}
  - \usepackage{fancyhdr} # package for fancy headers/footers. Following lines set options for this package
  - \renewcommand{\fancypagestyle}{}
  - \pagestyle{fancy}
  - \fancyhead{}
  - \fancyhead[L]{\textbf{Short Title}}  ## for Page Header
  - \fancyhead[R]{\textbf{Author Name}}  ## for Page Header
  - \fancyfoot{}
  - \renewcommand{\footrulewidth}{0.4pt}
  - \fancyfoot[R]{\textbf{Page \thepage}} 
  

Access to YAML meta-data

Specials

  • if section numbering is enabled in your YAML section (number_sections: yes), it can be disabled for particular sections with directive after the section header
# References {.unnumbered}
  • force references to be inserted in a specific place by including a pandoc fenced div at that location. You might do this, for example, if you want your references somewhere other than the very end of your document.
# References {.unnumbered}
::: {#refs}
:::

Rmarkdown Templates (not updated recently)

General Resources

Book/monograph formats

Web page formats

Presentation formats

Report/article formats

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment