How to use Cairo PNGs in R Markdown How to use Cairo PNGs in R Markdown r r

How to use Cairo PNGs in R Markdown


Use knitr options, not the YAML header.

You can use knitr options to change the type of a specific device (Yihui's recommendation):

knitr::opts_chunk$set(dev.args = list(png = list(type = "cairo")))

Alternately, you could do it conditionally based on the output:

if (!knitr::is_latex_output()) {  knitr::opts_chunk$set(dpi = 300, dev.args = list(type = "cairo"))})

I've used this on a couple of documents now. Note: I've only used this for documents doing rmarkdown::render(...) from the R command line.


As an alternative to @rmflight's answer using a piece of code at the top of each file, this can be achieved in the shell wrapper or Makefile that knits Rmarkdown files using the optional parameters to rmarkdown::render():

R -e 'rmarkdown::render("foo.Rmd", "pdf_document", output_file="foo.pdf", runtime = "static", output_options = list(dpi = 300, dev.args = list(type = "cairo")))'