由于 R 中的字体不可用,无法在四开版中生成报告

问题描述 投票:0回答:1

需要帮助...任何人都可以帮我解决我的问题...

由于字体问题,我在使用 RStudio 通过 Quarto 生成 .pdf 格式的报告时遇到问题。我相信这是由于

theme_ft_rc()
https://www.datacamp.com/promo/build-data-and-ai-skills-mar-24 使用了“Roboto Condensed”字体。在展示我的尝试之前,这就是我的命令的样子:

我的代码:

---
format: pdf
editor: visual
self_contained: TRUE
---

```{r message = FALSE, warning = FALSE, echo = FALSE}

plot_for_package <- function(package_code) {
     
gg <- gp.tb.pad.recipe %>%
  filter(Package_Code %in% package_code) %>%
  ggplot() +
  aes(x = Date, y = attemptR) +
  facet_wrap(vars(reorder(TESTER, -slope)), scales = "free_y") +
  geom_line(colour = "#58e6ed" , size = 1.5, alpha = 0.8) +
  geom_point(aes(fill = ifelse(Date == Sys.Date(), "orange", ifelse(attemptR > 1, "#Ea3c53", "#58e6ed"))), 
             colour = "yellow", shape = 21, size = 5 , alpha = 1) +
  theme_ft_rc() + # Dark theme based on FT’s dark theme
  scale_fill_ft() +
  scale_fill_identity() +
  geom_smooth(formula = y~x, method = "lm", se = FALSE, color = "#e966d7", linetype = "dashed", message = FALSE , size = 1)

gg
}
```

```{r fig.width = 15, fig.height = 9, dpi=300 , echo = FALSE ,warning =FALSE , message = FALSE}
lapply(pkg.c.pad, plot_for_package)
```

我的尝试1:

        library(extrafont)
        loadfonts(device = "win")   #"Roboto Condensed" 
    
    > loadfonts(device="win")
    Roboto Condensed already registered with windowsFonts().
    Agency FB already registered with windowsFonts().
    Algerian already registered with windowsFonts().
...
font_import(pattern="Roboto Condensed")

尝试 1 的错误消息:

> font_import(pattern="Roboto Condensed")
Importing fonts may take a few minutes, depending on the number of fonts and the speed of the system.
Continue? [y/n] y
Scanning ttf files in C:\Windows/Fonts, C:\Users\abc\AppData\Local/Microsoft/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

但是......我尝试了另一种方式,来自https://search.r-project.org/CRAN/refmans/hrbrthemes/html/import_roboto_condensed.html的建议......似乎字体确实存在?

> import_roboto_condensed()
You will likely need to install these fonts on your system as well.

You can find them in [C:/Users/int10176/AppData/Local/Programs/R/R-4.3.0/library/hrbrthemes/fonts/roboto-condensed]

尝试2,我尝试更改

theme_ft_rc()

中的字体
...
    theme_ft_rc(base_family = "Times New Roman") +
...

尝试 2 的错误消息:

Quitting from lines 339-340 [unnamed-chunk-7] (cny.qmd)
Error in `grid.Call.graphics()`:
! invalid font type
Backtrace:
  1. global .main()
 55. grid:::drawGrob(x)
 57. grid:::drawDetails.text(x, recording = FALSE)
 58. grid:::grid.Call.graphics(...)
There were 50 or more warnings (use warnings() to see the first 50)
Execution halted

第339-340行参考:

lapply(pkg.c.pad, plot_for_package)
r ggplot2 fonts rstudio quarto
1个回答
0
投票

我遇到了一些与此类似的问题,并且无法通过我看到的一些建议来解决它,例如使用

extrafont
等。

我有点放弃了,直到我随机遇到

library(ragg)

因此添加到我的文档中就解决了所有问题。

knitr::opts_chunk$set(dev = "ragg_png")

Ragg 有助于在渲染图像时访问系统字体。

© www.soinside.com 2019 - 2024. All rights reserved.