Search code examples
rfontsflextable

set up own font in flextable r


I am using flextable, and would like to use my own defined font. Is this possible?

I looked at Is it possible to change flextable default font from arial that says it is, but am not sure what fontname are available.

I have some fonts from my computer

enter image description here

but they are not recognized when I use them as a fontname and default arial is used. Is there a way to get it recognized by R?

head(iris) %>%
  flextable() %>%
  font(j=5,fontname='Rage Italic')

enter image description here

head(iris) %>%
  flextable() %>%
  style(j=5,pr_t=fp_text(color='purple',font.size=20,
                        font.family='Rage Italic'))

enter image description here

Any suggestions how to get this set up?


Solution

  • You can use systemfonts::system_fonts() to list all available fonts on your machine (note 'systemfonts' lets you add downloaded fonts as well). The column "family" is containing the values to match with font.family.

    systemfonts::system_fonts()
    #> # A tibble: 679 × 9
    #>    path                     index name  family style weight width italic monos…¹
    #>    <chr>                    <int> <chr> <chr>  <chr> <ord>  <ord> <lgl>  <lgl>  
    #>  1 /System/Library/Fonts/S…     2 Rock… Rockw… Bold  bold   norm… FALSE  FALSE  
    #>  2 /System/Library/Fonts/N…     0 Note… Notew… Light normal norm… FALSE  FALSE  
    #>  3 /Users/xxxxxxxxxx/Libra…     0 Fira… Fira … Ultr… light  norm… TRUE   FALSE  
    #>  4 /System/Library/Fonts/S…     1 Deva… Devan… Bold  bold   norm… FALSE  FALSE  
    #>  5 /Users/xxxxxxxxxx/Libra…     0 Fira… Fira … Thin  ultra… semi… FALSE  FALSE  
    #>  6 /System/Library/Fonts/S…     0 Verd… Verda… Bold  bold   norm… FALSE  FALSE  
    #>  7 /Users/xxxxxxxxxx/Libra…     3 Fira… Fira … Semi… semib… norm… FALSE  TRUE   
    #>  8 /System/Library/Fonts/S…     0 Kann… Kanna… Regu… normal norm… FALSE  FALSE  
    #>  9 /System/Library/Fonts/A…     8 Aria… Arial… Light light  norm… FALSE  FALSE  
    #> 10 /System/Library/Fonts/A…    10 Appl… Apple… Thin  thin   norm… FALSE  FALSE  
    #> # … with 669 more rows, and abbreviated variable name ¹​monospace
    

    Created on 2022-10-10 with reprex v2.0.2

    if( "Herculanum" %in% systemfonts::system_fonts()$family ){
      head(iris) %>%
        flextable() %>%
        style(j=5,pr_t=fp_text(color='purple',font.size=20,
                               font.family='Herculanum'))
      
    }
    

    enter image description here