Is it possible to change flextable default font from arial
Asked Answered
R

2

8

I love David Gohel's Officer and Flextable packages and they are really a flexible alternative to write word document reports with complex layouts which are not achievable in R markdown.

I have created custom styles for paragraphs and tables using officer, but now I am using flextable to customise the formatting on individual cells, although they always seem to be in arial font, regardless of the font in the word document (calibri).

Is there a particular style from the word document that flextable uses or is there a command to amend the font.

The closest I have come to an answer so far is the options("ReporteRs-default-font" = "Arial") function. Is there an equivalent command for officer or flextable?

Thanks very much for any guidance.

Remde answered 11/3, 2018 at 22:16 Comment(3)
There is a function named 'font` in the dev version on Github. Should be on CRAN soon.Bougainville
You can also use style and provide a fp_text object with the correct font nameBougainville
does font() allow me to change the default font for all flextables? I am thinking of something like options(flextable.font = "Times New Roman").Suppurative
I
8

The function font() will let you modify an individual cell's font (but not its color, size, etc.):

library(flextable)
library(magrittr) # for %>%
library(officer) # for fp_text
some_data <-iris[c(1,51,101),] 
# example: cell at (1,5)
flextable(some_data) %>% 
  font(i=1, j=5,fontname='Rage Italic')

Using fp_text() will allow you to modify font, boldness, size, color, etc.

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

enter image description here

Ithunn answered 17/10, 2018 at 18:40 Comment(0)
H
3

You need set_flextable_defaults(font.family = mybetterfont)

See the documentation for details.

Heathendom answered 4/8, 2021 at 22:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.