2

I have the following code which results in below fig. However I want the header_row to align center over the cols.(passed as arg.:colwidths, I've been searching, consulted the manual (but I'm new to R so my understanding is bad), but I'm at a lost. Everything else in the table should have the alignment it has.

ft <- flextable(df_BAS_sum)
ft <- set_caption(ft, caption =sprintf("Tabell 1. Arbetsmarknadsdata och nyckeltal för olika geografier, 20-64 år - %s %s", month, yr), align_with_table = F)
ft <- add_header_row(ft, colwidths = c(1,2,4,3), values = c("","Arbetskraften","Ej i arbetskraften","Nyckeltal"))
ft <- colformat_num(x = ft,
                    big.mark=" ", decimal.mark = ",",
                    na_str = "N/A")
ft <- theme_vanilla(ft)
ft

enter image description here

Thanks for any insight!

Christian
  • 117
  • 1
  • 3
  • 10
  • `ft <- align(ft, align = "center", part = "header")` does not do what you need? – Paul Stafford Allen Mar 01 '23 at 08:53
  • That also align the second row (which must be a part of the 'header') with e.g. "Geografi" onwards - I ideally does not want to change that 2nd rows alignment from left to center, only top (first) row. – Christian Mar 01 '23 at 09:13
  • 1
    I think you can specify individual header rows like `ft <- align(ft, i = 1, j = NULL, align = "center", part = "header")` - change the `i=` to select rows. Details here: https://rdrr.io/cran/flextable/man/align.html – Paul Stafford Allen Mar 01 '23 at 09:15
  • 1
    Wow that worked! I thought I tried that before, but must have missed something, anyways thanks mate! – Christian Mar 01 '23 at 09:23

1 Answers1

3
ft <- align(ft, i = 1, j = NULL, align = "center", part = "header")

part = "header" restricts it to the header rows only i = 1 (first row) j = NULL (all columns) should avoid the undesirable realignment of the other header row.

Paul Stafford Allen
  • 1,840
  • 1
  • 5
  • 16