How can I resolve the No Font Name issue when importing fonts into R using extrafont?
As it was mentioned by @Moritz Schwarz, the problem is traced to Rttf2pt1
.
According to a solution proposed here, downgrading it to 1.3.8 will fix the problem:
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
Downloading Fonts in R: 'No fontName. Skipping'
Using the following code worked when I downloaded Rtools40.
library(extrafont)
library(remotes)
remotes::install_version("Rttf2pt1", version = "1.3.8")
extrafont::font_import()
I cannot import fonts using font_import
Really check the file name
tl;dr Windows is case-insensitive, but R's grepl
is not, and import_font
passes the pattern argument grepl
Use:
extrafont::font_import(pattern="arial.ttf", prompt=FALSE)
Why? Because Windows returns "arial.ttf" as the file name.
File Explorer doesn't show font file names
The pattern "Arial.ttf" would match C:\Windows\Fonts\Arial.ttf
. However on my test system the file is just C:\Windows\Fonts\Arial
without an extension. This is what you see when looking at the directory using File Explorer. File Explorer is not showing you the file names as illustrated below.
Look for font file with R or powershell
The output of the file name via any of these methods is arial.ttf
.
Using powershell
ls C:\Windows\Fonts | findstr -i arial
gci -Path "C:\Windows\Fonts" -Recurse -File -Filter "arial.ttf"
gci -Path "C:\Windows\Fonts" -Recurse -File -Filter "Arial.ttf"
all show the filename arial.ttf
.
Using R
# font_import lists files using this function
list.files("C:/Windows/Fonts", pattern="\\.ttf") # shows arial.ttf
file.exists("C:/Windows/Fonts/arial.ttf") # TRUE
file.exists("C:/Windows/Fonts/Arial.ttf") # TRUE b/c Windows is case-insensitive
How font_import
uses pattern argument
Looking into how font_import
uses the pattern supplied:
extrafont::font_import # prints the source
#function (paths = NULL, recursive = TRUE, prompt = TRUE, pattern = NULL)
#{
#...
# ttf_import(paths, recursive, pattern)
#}
extrafont:::ttf_import # print the source
#function (paths = NULL, recursive = TRUE, pattern = NULL)
#{
# if (is.null(paths))
# paths <- ttf_find_default_path()
# ttfiles <- normalizePath(list.files(paths, pattern = "\\.ttf$",
# full.names = TRUE, recursive = recursive, ignore.case = TRUE))
# if (!is.null(pattern)) {
# matchfiles <- grepl(pattern, basename(ttfiles))
# ttfiles <- ttfiles[matchfiles]
# }
#...
#}
The line where the supplied pattern gets used is in a call to grepl
matchfiles <- grepl(pattern, basename(ttfiles))
Cannot import fonts into R
The best solution to my knowledge is showtext
and sysfonts
. First, add the font to the session:
# directly from google fonts
sysfonts::font_add_google("Roboto Condensed")
# or add an arbitrary font with
sysfonts::font_add("Roboto Condensed", regular = "RobotoCondensed-Regular.ttf")
Once this is done, simply load showtext
and run showtext_auto()
once to activate it (you need to repeat add_font*
and showtext_auto
every session):
library(ggplot2)
library(showtext)
showtext_auto()
ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
labs(x="Fuel efficiency (mpg)", y="Weight (tons)",
title="Seminal ggplot2 scatterplot example",
subtitle="A plot that is only useful for demonstration purposes",
caption="Brought to you by the letter 'g'") +
hrbrthemes::theme_ipsum_rc()
Created on 2022-03-22 by the reprex package (v2.0.1)
You can also try to make the font available permanently. But it seems to be hit and miss. Theoretically, you need to install the font into C:\Windows\Fonts
, which you can do by 1) unzip the fonts, 2) right click and "install for all users". If you install the fonts in a different way, there is a good chance Windows will only put a link into C:\Windows\Fonts
, which R can't deal with.
You can check available fonts with:
fonts_df <- sysfonts::font_files()
View(fonts_df)
This works for most fonts in my experience but with Roboto, I still had no luck for some reason. add_font*
seems to be the best way to go for now.
How to solve the error status 5 in extrafont?
I suspect this is related to issues with Rttf2pt1 (upon which extrafont depends) as described here and here. Suggested solution which worked for me is to downgrade Rttf2pt1 as below:
remove.packages("Rttf2pt1")
remotes::install_version("Rttf2pt1", version = "1.3.8")
Adding Fonts to R using extrafont or showtext libraries (on Mac via FontBook)
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.
R package extrafont::font_import() not finishing
The font_import
function gives a prompt on the console that needs to be answered with y
or n
. Hence, it was waiting for a response, and nothing happened.
(Posting answer since OP cannot post themselves.)
Related Topics
Plotting During a Loop in Rstudio
Current Time in Iso 8601 Format
R * Not Meaningful for Factors Error
Plot Background Colour in Gradient
R Dplyr Rowwise Mean or Min and Other Methods
Calculating Time Difference Between Two Columns
Assign a Value, If a Number Is in Between Two Numbers
R: How to Use Coord_Cartesian on Facet_Grid with Free-Ranging Axis
Adding Empty Graphs to Facet_Wrap in Ggplot2
Identify Duplicates and Mark First Occurrence and All Others
Replace Missing Values (Na) in One Data Set with Values from Another Where Columns Match
Merging a Large List of Xts Objects
R: Using a String as an Argument to Mutate Verb in Dplyr
How to Cumulatively Add Values in One Vector in R