Reputation: 787
I am trying to import fonts for ggplot2 graphs as in described here.
When I try do to so piece by piece with this code :
font_import(pattern = "Arial.ttf")
y
I get this error :
canning ttf files in C:\windows\Fonts ...
Extracting .afm files from .ttf files...
Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 1
I have checked that I have indeed the Arial font :
What is my problem
Upvotes: 3
Views: 5686
Reputation: 71
I was having the same problem, i.e. trying to import a font (Zallman Caps) using font_import()
and receiving the same error:
"Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 1"
After a lot of trial and error, I finally found a solution that worked for me and perhaps it will for you, as well.
Even though I could see the Zallman Caps font in my Windows Fonts folder and verified that it worked in Word, neither font_import()
nor list.files()
could find it there. I managed to import the font by entering the following code:
font_import(path = "C:/Users/*insert your user name*/AppData/Local/Microsoft/Windows/Fonts", pattern = ".TTF")
I have no idea why it isn't visible to R in the main fonts directory, when I can clearly see it in Windows file explorer, but at least I managed to import the font using this workaround.
Upvotes: 7
Reputation: 617
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.
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.
The output of the file name via any of these methods is arial.ttf
.
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
.
# 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
font_import
uses pattern argumentLooking 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))
Upvotes: 2