rheaderfont-sizer-flextable

How to have multiple font sizes in a flextable header


I have a flextable() where I want to make the header have two font sizes. One size for the main text (say, 20 pt font), and a smaller for the parenthetical text (12 pt font). Here's code that makes the entire header 20 pt.

Note: In the final solution, I'd prefer not to make the parenthetical text an entirely new row unless it can be as near to the original text as the below example shows.

library(flextable)
library(dplyr)

set_flextable_defaults(font.size = 20) #Works but makes all header size 20

flextable(test) %>%
  set_header_labels(values = list(name = "Name", med_score = "Score (Median)", mean_score = "Score (Mean)")) %>%
  align(align = "center", part = "header")

Here's what it looks like currently:

enter image description here

And here's what I want it to look like:

enter image description here

Note: I made this goal table in PowerPoint. It looks different in many ways from the original one because I haven't formatted the original. The only difference that I'm trying to replicate is having (median) and (mean) being smaller than Score as well as them being in the same header cell.


Solution

  • You could use compose, as_paragraph and as_chunk to achieve it.

    For reference: https://ardata-fr.github.io/flextable-book/cell-content-1.html#multi-content

    library(flextable)
    
    ft <- flextable(airquality[ sample.int(10),])
    
    compose(
      ft,
      j = "Ozone",
      part = "header",
      value = as_paragraph(
        "Ozone ",
        as_chunk(
          " (Median)",
          props = fp_text_default(color = "#006699", font.size = 5)
        )
      )
    )
    

    enter image description here