Save Custom Fonts in R package

Hi everyone,

I'm developing an internal package for my organization. We've decided to start with some basic functions that will help use deliver more consistent visualizations using our corporate image (vector with corporate color codes, logos, functions to standarized ggplots, etc).

We have our own custom font which is 'Montserrat'. It's a google font so we can even use it with R Markdown files. Nevertheless, we algo use it with ggplot graphs so we can keep our visualizations clean and standarized. In order to do that we have a local folder with out fonts with '.ttf' formats.

For the first time using this font we make a standard procedure which implies that each user downloads the folder available in the cloud with the font, installs it and then run the following code from the extrafont package:

# Install the package if needed 
install.packages('extrafont')


#Load the package
library(extrafont)

# this funciton imports the fonts 
font_import()

loadfonts(device = "win")

I would like to know if there is a way in which I could automate the entire process and have the font files save into my R Package. In this way the process of importing the brand's font would look something like:

  1. Install Internal Package
  2. Run Function called: setup_font() ( Or whatever name we assign)
  3. This function now goes through the files inside the R Package, look for the fonts, then install the library extrafont if necessary. Solve any issues ( sometimes you have to install the following package because it may cause conflicts:
remotes::install_version("Rttf2pt1", version = "1.3.8")
  1. Install the fonts in the user's pc
  2. Import Fonts to extrafont

Is that Possible? If it is, could you guide me into how to do something like this? How should i save the fonts in the package, etc.

I would really appreciate it. I haven't found any help in other forums.

Thank you.

As an alternative to extrafont, consider showtextas a dependency. Then your setup() script is just

library(showtext)
font_add_google("Montserrat", "montserrat")

CAUTION: running this script will install a google font on your system

library(ggplot2)
library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Montserrat", "montserrat")

## Automatically use showtext to render text
showtext_auto()

some_text <- "This line illustrates the use of the Montserrat font in text"

the_plot <- ggplot(NULL, aes(x = 1, y = 1)) + 
  ylim(0.8, 1.2) +
  labs(title = "Use of google fonts in ggplot2",
       subtitle = "Adapted from https://journal.r-project.org/archive/2015-1/qiu.pdf") +
  annotate("text", 1, 1, label = "A sample of\nMontserrat", family = "montserrat", size = 15) +
  theme_minimal()

the_plot

2 Likes

This is of course for a standalone script. For integration into your internal package you would:
Add {showtext} to Imports, either create a setup function or also add sysfont::font_add_google to .onload and add showtext_auto to .onload (or maybe you need to use showtext_begin/end within your visualization functions, idk as I have not used {showtext} yet)

2 Likes

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.