Adding Fonts to R using extrafont or showtext libraries (on Mac via FontBook)

3.7k views Asked by At

Two years ago I posted a related post: Add font to R that is not in extrafonts library. With a better understanding of how fonts works, I now have a new set of fonts that I need to upload into R to use with ggplot. Despite the useful answers in the previous post, I'm running into a new set of issues:

First, I cannot load the showtext library. After reinstalling and loading the library, I receive this error message:

>   library(showtext)
Loading required package: sysfonts
Error: package or namespace load failed for ‘sysfonts’:
 .onLoad failed in loadNamespace() for 'sysfonts', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so, 6): Library not loaded: /opt/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so
  Reason: image not found
Error: package ‘sysfonts’ could not be loaded

...and when I try to load the sysfonts library, I receive the following:

> library(sysfonts)
Error: package or namespace load failed for ‘sysfonts’:
 .onLoad failed in loadNamespace() for 'sysfonts', details:
  call: dyn.load(file, DLLpath = DLLpath, ...)
  error: unable to load shared object '/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so, 6): Library not loaded: /opt/X11/lib/libfreetype.6.dylib
  Referenced from: /Library/Frameworks/R.framework/Versions/3.5/Resources/library/sysfonts/libs/sysfonts.so
  Reason: image not found

I am not sure how to fix this unable to load shared object error, and as a result I've mostly given up on using showtext, as I cannot get it loaded into R.

Moving onto extrafont then, I run the following, to import fonts from my Mac into R:

> library(extrafont)
> font_import() # import all fonts on my system
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y
Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
Extracting .afm files from .ttf files...
/Library/Fonts/Andale Mono.ttf => /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics/Andale Mono
/Library/Fonts/Apple Chancery.ttf : No FontName. Skipping.
/Library/Fonts/AppleGothic.ttf : No FontName. Skipping.
/Library/Fonts/Arial Black.ttf => /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics/Arial Black
...
...
Found FontName for 63 fonts.
Scanning afm files in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/metrics
Writing font table in /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/fontmap/fonttable.csv
Writing Fontmap to /Library/Frameworks/R.framework/Versions/3.5/Resources/library/extrafontdb/fontmap/Fontmap...
There were 50 or more warnings (use warnings() to see the first 50)

I notice that certain fonts are skipped due to a No FontName error, whereas others (Arial Black) seem to load fine. After running the font_import, I check to see the fonts available:

>   fonts()
 [1] ".Keyboard"             "System Font"           ".SF NS Rounded"        "Andale Mono"          
 [5] "Apple Braille"         "AppleMyungjo"          "Arial Black"           "Arial"                
 [9] "Arial Narrow"          "Arial Rounded MT Bold" "Arial Unicode MS"      "Bodoni Ornaments"     
[13] "Bodoni 72 Smallcaps"   ""                      "Brush Script MT"       "Comic Sans MS"        
[17] "Courier New"           "DIN Alternate"         "DIN Condensed"         "Georgia"              
[21] "Impact"                "Khmer Sangam MN"       "Lao Sangam MN"         "Luminari"             
[25] "Microsoft Sans Serif"  "Tahoma"                "Times New Roman"       "Trattatello"          
[29] "Trebuchet MS"          "Verdana"               "Webdings"              "Wingdings"            
[33] "Wingdings 2"           "Wingdings 3"    

For my custom fonts, I was provided with a zip file of fonts, and I have uploaded those fonts to my Mac*. The font name is FreightDisp Pro, and I can see here that the font is in Mac's Font Book app. However, I would note that the font is not available in my library/Fonts/ folder, as you can see in the following image:

enter image description here enter image description here

I've tested that the font is available in Microsoft Word, and it is.

And Finally, when I attempt to import the fonts into R, I receive the following error:

> font_import(pattern="FreightDisp Pro")
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y
Scanning ttf files in /Library/Fonts/, /System/Library/Fonts, ~/Library/Fonts/ ...
Extracting .afm files from .ttf files...
Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 0, 1

and I receive this same error when changing the pattern to "National2" as well.

So as I conclude this post, it seems that I also have arrived at the answer to my question, which is that the fonts cannot simply be installed in fontbook, but must also be in the /Library/Fonts folder. I am not sure if I can simply drag + drop the fonts into this folder, or if I can use Font Book to write these fonts into the correct locations. I'm also not sure about the file types, if I can only use .ttf files as the post suggests, even though the font files I have are all .otf.

Edit

Using custom OTF fonts in ggplot2 this post indicates that .otf fonts would need to be converted to .ttf to use with extrafonts, but that with showtext, .otf fonts can be used. So I will try to load that library.

1

There are 1 answers

0
Canovice On BEST ANSWER

enter image description here In Font Book > Preferences, by switching the preference of the fonts from User to Computer, it ensures that the fonts install into the fonts folder. This solved my problem.