gpt4 book ai didi

ggplot中的Rscript无效字体类型

转载 作者:行者123 更新时间:2023-12-03 17:31:18 24 4
gpt4 key购买 nike

我有一个使用非标准字体生成 ggplot 的简短脚本(请参阅下面的 MWE)。
如果我在 R(即 RStudio)中运行脚本,一切都会按预期工作,而且我看不到任何错误。
当我使用命令行和 Rscript 运行脚本时我收到错误:

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y,  : 
invalid font type
Calls: <Anonymous> ... drawDetails -> drawDetails.text -> grid.Call.graphics
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted
我用 Rscript myscript.R 执行脚本,脚本的内容是
library(ggplot2)
theme_set(theme_light(base_family = "LM Roman 10"))
# if "base_family" is left empty, everything works with Rscript

p <- ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point()

ggsave("myplot.pdf", p, device = cairo_pdf)
我使用 Ubuntu 18.04.1 和 R 3.5.1。我还更新了我所有的软件包。
当我使用 R CMD BATCH myscript.R一切正常。
临时解决方案
如果你有同样的问题(到目前为止我还没有解决),我已经恢复使用 R CMD BATCH --vanilla myscript.R .

最佳答案

我发现,如果您使用 extrafont 库将所有​​系统字体导入 R,则往往可以解决问题。
最简单的方法是在终端中运行 R 并执行类似的操作

$ R

R version 3.6.3 (2020-02-29) -- "Holding the Windsock"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

> library(extrafont)
Registering fonts with R
> extrafont::font_import()
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y

按回车后,您的所有系统字体都应该被导入。
另一种可能是你需要为Ghostscript设置一个环境变量,这可以通过在你的库调用后添加以下内容来完成
Sys.setenv(R_GSCMD="/usr/bin/ghostscript")

关于ggplot中的Rscript无效字体类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53723752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com