If a Google Font is requested as part of a thematic theme, thematic attempts to download, cache, and register that font for use with showtext and ragg before plotting (see
font_spec() for more details). Therefore, to correctly render these fonts, you’ll need at least showtext or ragg installed; and in some scenarios, some additional configuration may be required.
showtext allows custom fonts to be used with essentially any R graphics device, whereas ragg itself provides (high-quality and cross-platform) bitmap devices with 1st class support for custom fonts. Therefore, consider using showtext for vector-based images (i.e.,
cairo_pdf(), etc) and ragg for raster-based images (i.e.,
ragg::agg_jpeg(), etc). Note that
shiny::renderPlot() currently requires a png image, so ragg is generally preferable to showtext in that case, especially on hosted environments (e.g., Connect, shinyapps.io, etc. – because they don’t have support for the high-quality
If you have showtext installed, Google Fonts should ‘just work’ by default inside Shiny and R Markdown. However, before deployment, include
library(showtext) in the app/document to make sure it’s installed on the hosted instance. Also, if you encounter issues in R Markdown, make sure thematic is being loaded in an initial “setup” chunk prior to any plotting chunks (loading thematic ensures that the
fig.showtext knitr option defaults to
TRUE in future code chunks).
Using ragg instead of showtext in these contexts requires some additional configuration: * Set
options(shiny.useragg = TRUE) in Shiny (requires shiny v1.5.0 or higher). * Set
knitr::opts_chunk$set(dev = "ragg_png") in R Markdown (requires knitr v1.29 or higher).
In theory, as long as showtext is installed, Google Fonts should render on any R graphics device. Here’s a mini example with
thematic_on("black", "white", font = "Pacifico") tmp <- tempfile(fileext = ".svg") svglite::svglite(tmp, width = 7, height = 3) plot(1, type = "n") text(1, "Pacifico font") dev.off() if (rstudioapi::isAvailable()) file.show(tmp)
Note that thematic also includes a
thematic_save_plot() that is essentially a convenience function for saving a plot to a graphics device, but also does the following:
res), which is important to get the font sizes right in bitmap images (e.g., png, jpeg, etc).
Other fonts that already known to R require no additional setup for use with thematic. One way to make a font family generally available to R is to import them via the extrafont package. You can also register a font for use with showtext via
sysfonts::font_add() and with ragg via
systemfonts::register_font(). The benefit of the sysfonts/systemfonts approach is that registration is much much faster, and generally works better, but registration must also be (re)-done in every new R session.