"

Adding a function which detects the client’s OS and silently loads fonts accordingly

Using custom fonts in #rstats can be tricky, specially if the fonts need to use a variant on the standard family (e.g. Medium) which can be called different things across Windows and Mac.
Author
Affiliation

Building Stories with Data

Published

December 16, 2022

When building a package for clients which applies a custom theme, I was keen to make it as easy as possible. But particular issue caused a few headaches. I adapted this solution by June Choe to add a simple .onLoad() function to the package - problem solved!

The challenge was tied to Mac and Windows dealing differently with spaces in font names. Working across different operating systems at rfortherest showed us where the problem was. The package now includes a function which detects the OS and silently loads fonts accordingly 🥳

Adding an .onLoad() function to the package is a really useful trick that I’ll be using again! Really easy way to set things up nicely without having to remind clients to run X, Y or Z function before making use of the rest of the package. But with one key caveat. If the font isn’t installed on the client’s device, the .onLoad() function produces an error when the client tries to install the package - once we figured that out, it more plain sailing!

Every day an #rstats school day!

Reuse

Citation

For attribution, please cite this work as:
Thompson, Cara. 2022. “Adding a Function Which Detects the Client’s OS and Silently Loads Fonts Accordingly.” December 16, 2022. https://www.cararthompson.com/posts/2022-12-16-using-custom-fonts-in-rstats/.