Add a row of new columns labels in header part. Labels can be spanned along multiple columns, as merged cells.

Labels are associated with a number of columns to merge that default to one if not specified. In this case, you have to make sure that the number of labels is equal to the number of columns displayed.

The function can add only one single row by call.

Labels can also be formatted with as_paragraph().

add_header_row(x, top = TRUE, values = character(0), colwidths = integer(0))

Arguments

x

a flextable object

top

should the row be inserted at the top or the bottom. Default to TRUE.

values

values to add, a character vector (as header rows contains only character values/columns), a list or a call to as_paragraph().

colwidths

the number of columns used for each label

Examples

library(flextable)

ft01 <- fp_text_default(color = "red")
ft02 <- fp_text_default(color = "orange")

pars <- as_paragraph(
  as_chunk(c("(1)", "(2)"), props = ft02), " ",
  as_chunk(c("My tailor is rich",
    "My baker is rich"), props = ft01)
)

ft_1 <- flextable(head(mtcars))
ft_1 <- add_header_row(ft_1, values = pars,
  colwidths = c(5, 6), top = FALSE)
ft_1 <- add_header_row(ft_1, values = pars,
  colwidths = c(3, 8), top = TRUE)
ft_1
#> a flextable object.
#> col_keys: `mpg`, `cyl`, `disp`, `hp`, `drat`, `wt`, `qsec`, `vs`, `am`, `gear`, `carb` 
#> header has 3 row(s) 
#> body has 6 row(s) 
#> original dataset sample: 
#>                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
#> Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
#> Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
#> Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
#> Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
#> Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

ft_2 <- flextable(head(airquality))
ft_2 <- add_header_row(ft_2, values = c("Measure", "Time"),
  colwidths = c(4, 2), top = TRUE)
ft_2 <- theme_box(ft_2)
ft_2
#> a flextable object.
#> col_keys: `Ozone`, `Solar.R`, `Wind`, `Temp`, `Month`, `Day` 
#> header has 2 row(s) 
#> body has 6 row(s) 
#> original dataset sample: 
#>   Ozone Solar.R Wind Temp Month Day
#> 1    41     190  7.4   67     5   1
#> 2    36     118  8.0   72     5   2
#> 3    12     149 12.6   74     5   3
#> 4    18     313 11.5   62     5   4
#> 5    NA      NA 14.3   56     5   5