Search code examples
rggtext

Verify font availability in R


I have a font installed on my OS, so this code runs fine:

library(tidyverse)
library(extrafont)
iris %>% ggplot(aes(Sepal.Length,Sepal.Width, color = Species)) + 
  geom_point(size = 2) + 
theme(
  text = element_text(family = "Metropolis")
)

Let's force an error (note that I wrote "metropolis", not "Metropolis"):

iris %>% ggplot(aes(Sepal.Length,Sepal.Width, color = Species)) + 
  geom_point(size = 2) + 
theme(
  text = element_text(family = "metropolis")
)

That gives me an error, which is ok because font "metropolis" doesn't exist.

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  polygon edge not found

Is there a way where I can verify before if certain font is installed, in R? Thank you in advance.


Solution

  • You're already using the extrafont package so you can use fonts() to see the registered fonts. To check if a particular font is available you can do:

    library(extrafont)
    
    "metropolis" %in% fonts()
    
    [1] FALSE