6

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.

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
Rich Tyler
  • 61
  • 1
  • 2
  • There is a function named 'font` in the dev version on Github. Should be on CRAN soon. – David Gohel Mar 12 '18 at 12:55
  • You can also use `style` and provide a `fp_text` object with the correct font name – David Gohel Mar 12 '18 at 12:56
  • 1
    does `font()` allow me to change the *default* font for all flextables? I am thinking of something like options(flextable.font = "Times New Roman"). – JBJ Jul 02 '19 at 13:14

2 Answers2

7

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

C8H10N4O2
  • 18,312
  • 8
  • 98
  • 134
1

You need set_flextable_defaults(font.family = mybetterfont)

See the documentation for details.

Alpi Murányi
  • 1,117
  • 10
  • 17