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 = "--"
)
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\\&
.
- See notes on figure and table numbering in this gist: https://gist.github.com/davebraze/64de1a02812cf9da3d97f2f8072c3712
- Sometimes, inserting a
\clearpage
directive right after a codeblock that creates a figure in a pdf file may help to keep the figure in place, relative to the surrounding text.
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}}
- See notes in this gist: https://gist.github.com/davebraze/d15f9b5adeab4c1373e80f068dea12a4
- Notes on interpreted R code in yaml block: https://stackoverflow.com/questions/44464742/inline-r-code-in-yaml-block-of-rmarkdown-file
- 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}
:::
- Document template gallery: https://rmarkdown.rstudio.com/gallery.html
- Rmarkdown cookbook in dev: https://github.com/dr-harper/rmarkdown-cookbook
- Bookdown: https://github.com/rstudio/bookdown
- Blogdown: https://github.com/rstudio/blogdown
- rmdformats: https://github.com/juba/rmdformats
- prettydoc: https://github.com/yixuan/prettydoc
- prettyjekyll: https://github.com/privefl/prettyjekyll
- xaringan: https://github.com/yihui/xaringan
- Tufte Handout: https://rstudio.github.io/tufte/
- J. of Statistical Software: https://github.com/rstudio/rticles/tree/master/inst/rmarkdown/templates/jss_article
- Papaja is an APA-centric templating system, available at https://github.com/crsh/papaja . Papaja includes an R Markdown template that can be used with (or without) RStudio to make documents that conform to APA manuscript guidelines (6th Edition). The package uses the LaTeX document class apa6 to build LaTeX and PDF files, and includes a .docx-reference file so you can make Word documents if you must.
- article templates: https://github.com/rstudio/rticles