2

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.

Alexis
  • 2,104
  • 2
  • 19
  • 40

1 Answers1

4

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
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
  • When I tried `"Metropolis" %in% fonts()`it gave me `FALSE`. Am I doing anything wrong? – Alexis Jun 14 '20 at 01:28
  • 1
    Additional fonts need to be registered with `font_import()` before they're available. See https://stackoverflow.com/questions/52251290/add-font-to-r-that-is-not-in-extrafonts-library – Ritchie Sacramento Jun 14 '20 at 01:35
  • Realized that `fonts()`only shows ttf fonts. Now it works. Thank you. – Alexis Jun 14 '20 at 12:11