R: Set Font Based on Output Device

Ever had a troublesome font in R that doesn’t want to render to ggplot in RStudio, but will render just fine in knitr? Do you use extrafont but sometimes it just doesn’t work? Trying to look like Tufte’s Visual Display of Quantitative Information but ETBembo is being uncooperative? Try setting your font (globally) to this.

myfont <- ifelse(is.null(knitr::opts_knit$get("rmarkdown.pandoc.to")), "serif", "ETBembo")

This sets myfont to serif if the code is being rendered in anything but knitr. If it is being rendered by knitr, say, by pressing the “Knit” button in RStudio, then it sets it to ETBembo.

I found it on StackOverflow.

Subscribe to Sara Burke

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe