Check to see if font is available for ggplot

Hi there!

Fonts in R confuse me. In a package I'm authoring I want to create a ggplot theme that will use 'Lato' if the font is available for R on the system, and 'sans' if not (since sans is available on every system, right?).

Pseudo-code wise, I imagine it might look something like this:

system_font <- if ('Lato' %in% fonts) 'Lato' else 'sans'
new_theme <- function(base_family = system_font) {
    theme_grey(base_family = base_family) +
    theme(...) #other stuff

What's the best way for me to do that, without usage of the extrafont package? To my understanding, with extrafont the first line could be written as:

system_font <- if ('Lato' %in% extrafont::fonttable()$FamilyName) 'Lato' else 'sans'

but I'm hoping not to add an additional dependency to this package. Is there a way to check for fonts that are available to ggplot without extrafont? Or maybe even to somehow specify a "fallback" font for ggplot to use in case 'Lato' isn't there?


1 Like