[英]How can I check whether a given font has been loaded already using extrafont package in R?
I've written a function to share among colleagues for graphing, and my organization prefers Calibri to the ggplot2 default Arial for the text.我已经写了一个 function 来与同事分享作图,我的组织更喜欢 Calibri 而不是 ggplot2 默认的 Arial 文本。 If I were the only person who'd be using this function, I would first do this at the top of my script:
如果我是唯一使用这个 function 的人,我会首先在我的脚本顶部执行此操作:
library(extrafont)
font_import()
loadfonts(device="win")
and then make my ggplot2 graph.然后制作我的 ggplot2 图。 I seem to only have to do
font_import()
once on a given machine, but then I need to do loadfonts(device="win")
each time I start a new session. I'm not terribly well versed in what these do, but if I don't do the loadfonts
step, my graph doesn't use Calibri.我似乎只需要在给定的机器上执行一次
font_import()
,但是每次我启动一个新的 session 时我都需要执行loadfonts(device="win")
。我不是很精通这些做什么,但如果我不执行loadfonts
步骤,我的图表就不会使用 Calibri。
I'd like the graphing function I wrote to work for others, and I'd like to check whether they've already done these steps and give them a helpful message about it if they haven't.我希望我写的图表 function 可以为其他人工作,我想检查他们是否已经完成这些步骤,如果他们没有,就给他们一个有用的信息。 I thought I could use
fonts()
and then check whether Calibri was listed in the output, but I think that only checks what fonts I've ever loaded with font_import()
in the history of using this machine.我以为我可以使用
fonts()
然后检查 Calibri 是否列在 output 中,但我认为这只会检查我在使用这台机器的历史中用font_import()
加载过的 fonts。 I also thought maybe我也想也许
systemfonts::match_font("Calibri")
would check, but I get the same result regardless of whether I've already run loadfonts(...
, so that's not it, either.会检查,但无论我是否已经运行
loadfonts(...
,我都会得到相同的结果,所以也不是。
How do you check whether a font is ready to be used in a graph?您如何检查字体是否已准备好在图形中使用?
Here is one potential approach to determine whether Calibri is installed and 'useable':这是确定 Calibri 是否已安装且“可用”的一种可能方法:
install.packages("showtext")
library(showtext)
list_of_fonts <- as.data.frame(font_files())
grep(pattern = "Calibri", x = list_of_fonts$family, ignore.case = TRUE, value = TRUE)
You can implement this in a number of ways, eg load the Calibri font if it's available or print a message if it's not available on the system:您可以通过多种方式实现这一点,例如,如果 Calibri 字体可用,则加载该字体;如果系统不可用,则打印一条消息:
if (!require(showtext)) install.packages("showtext")
#> Loading required package: showtext
#> Loading required package: sysfonts
#> Loading required package: showtextdb
library(showtext)
list_of_fonts <- as.data.frame(font_files())
if(any(grepl("Calibri.ttf", list_of_fonts, ignore.case = TRUE))){
Calibri <- list_of_fonts[list_of_fonts$file == "Calibri.ttf",]
sysfonts::font_add(family = "Calibri",
regular = list.files(path = Calibri$path,
pattern = "Calibri.ttf",
full.names = TRUE))
print("Calibri available")
} else{
print("Calibri not found")
}
#> [1] "Calibri available"
library(ggplot2)
showtext_auto()
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
ggtitle("Example plot") +
theme(text = element_text(family = "Calibri", size = 22))
Created on 2021-09-29 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2021-09-29
NB This should work on windows/macOS/linux but I've only tested it on macOS.注意这应该适用于 windows/macOS/linux,但我只在 macOS 上测试过。 Also, @JonSpring commented first, so if he posts an answer please accept his instead of mine
另外,@JonSpring 首先评论,所以如果他发布了答案,请接受他的而不是我的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.