Search code examples
rggplot2fontsrstudioquarto

Unable generate report in Quarto due to font unavailable in R


need help... can anyone help me to solve my problem...

I have a problem in generate the report in .pdf format through Quarto by using RStudio, due to the font issue. I believed that's due to the theme_ft_rc() https://www.datacamp.com/promo/build-data-and-ai-skills-mar-24 using the font "Roboto Condensed". Before showing my attepmt, this is how my command looks like :

My code :

---
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)
```

My attempt 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")

Error message for attempt 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

But... I tried another way which the suggestion from https://search.r-project.org/CRAN/refmans/hrbrthemes/html/import_roboto_condensed.html ... seems like the font actually exist?

> 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]

Attempt 2 , I tried to change the font in theme_ft_rc()

...
    theme_ft_rc(base_family = "Times New Roman") +
...

Error messagae for attempt 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

Line 339-340 refer to :

lapply(pkg.c.pad, plot_for_package)

Solution

  • I had some similar issues to this, and wasn't able to resolve it with some of the advice I'd seen like using extrafont etc.

    I had sort of given up until I randomly came across library(ragg)

    And so adding to my document fixed everything.

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

    Ragg facilitates access to system fonts when rendering images.