我无法使用 font_import 导入字体
I cannot import fonts using font_import
我正在尝试为 ggplot2 图形导入字体,如 here 所述。
当我尝试使用这段代码一块一块地做时:
font_import(pattern = "Arial.ttf")
y
我收到此错误:
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
我检查过我确实有 Arial 字体:
我的问题是什么
真的检查文件名
tl;dr Windows不区分大小写,但是R的grepl
不区分大小写,import_font
传递模式参数grepl
使用:
extrafont::font_import(pattern="arial.ttf", prompt=FALSE)
为什么?因为Windowsreturns"arial.ttf"作为文件名.
文件资源管理器不显示字体文件名
模式 "Arial.ttf" 将匹配 C:\Windows\Fonts\Arial.ttf
。但是在我的测试系统上,文件只是 C:\Windows\Fonts\Arial
没有扩展名。这是您在使用文件资源管理器查看目录时看到的内容。文件资源管理器未向您显示如下所示的文件名。
使用 R 或 powershell 查找字体文件
通过任何这些方法输出的文件名是arial.ttf
。
使用 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"
全部显示文件名arial.ttf
.
使用 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
font_import
如何使用模式参数
研究 font_import
如何使用提供的模式:
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]
# }
#...
#}
使用提供的模式的行是在对 grepl
的调用中
matchfiles <- grepl(pattern, basename(ttfiles))
我遇到了同样的问题,即尝试使用 font_import()
导入字体 (Zallman Caps) 并收到相同的错误:
"Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 1"
经过大量的反复试验,我终于找到了适合我的解决方案,也许它也适合你。
尽管我可以在我的 Windows 字体文件夹中看到 Zallman Caps 字体并验证它在 Word 中有效,但 font_import()
和 list.files()
都无法在那里找到它。我通过输入以下代码成功导入了字体:
font_import(path = "C:/Users/*insert your user name*/AppData/Local/Microsoft/Windows/Fonts", pattern = ".TTF")
我不知道为什么它在主字体目录中对 R 不可见,而我可以在 Windows 文件资源管理器中清楚地看到它,但至少我设法使用此解决方法导入字体.
我正在尝试为 ggplot2 图形导入字体,如 here 所述。
当我尝试使用这段代码一块一块地做时:
font_import(pattern = "Arial.ttf")
y
我收到此错误:
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
我检查过我确实有 Arial 字体:
我的问题是什么
真的检查文件名
tl;dr Windows不区分大小写,但是R的grepl
不区分大小写,import_font
传递模式参数grepl
使用:
extrafont::font_import(pattern="arial.ttf", prompt=FALSE)
为什么?因为Windowsreturns"arial.ttf"作为文件名.
文件资源管理器不显示字体文件名
模式 "Arial.ttf" 将匹配 C:\Windows\Fonts\Arial.ttf
。但是在我的测试系统上,文件只是 C:\Windows\Fonts\Arial
没有扩展名。这是您在使用文件资源管理器查看目录时看到的内容。文件资源管理器未向您显示如下所示的文件名。
使用 R 或 powershell 查找字体文件
通过任何这些方法输出的文件名是arial.ttf
。
使用 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"
全部显示文件名arial.ttf
.
使用 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
font_import
如何使用模式参数
研究 font_import
如何使用提供的模式:
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]
# }
#...
#}
使用提供的模式的行是在对 grepl
matchfiles <- grepl(pattern, basename(ttfiles))
我遇到了同样的问题,即尝试使用 font_import()
导入字体 (Zallman Caps) 并收到相同的错误:
"Error in data.frame(fontfile = ttfiles, FontName = "", stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 1"
经过大量的反复试验,我终于找到了适合我的解决方案,也许它也适合你。
尽管我可以在我的 Windows 字体文件夹中看到 Zallman Caps 字体并验证它在 Word 中有效,但 font_import()
和 list.files()
都无法在那里找到它。我通过输入以下代码成功导入了字体:
font_import(path = "C:/Users/*insert your user name*/AppData/Local/Microsoft/Windows/Fonts", pattern = ".TTF")
我不知道为什么它在主字体目录中对 R 不可见,而我可以在 Windows 文件资源管理器中清楚地看到它,但至少我设法使用此解决方法导入字体.