The function adds a list of values to be inserted as new rows in the header. The values are inserted in existing columns of the input data of the flextable. Rows can be inserted at the top or the bottom of the header.
If some columns are not provided, they will be replaced by
NA
and displayed as empty.
Arguments
- x
a flextable object
- top
should the rows be inserted at the top or the bottom.
- ...
named arguments (names are data colnames) of values to add. It is important to insert data of the same type as the original data, otherwise it will be transformed (probably into strings if you add a
character
where adouble
is expected). This makes possible to still format cell contents with thecolformat_*
functions, for examplecolformat_num()
.- values
a list of name-value pairs of labels or values, names should be existing col_key values. This argument can be used instead of
...
for programming purpose (Ifvalues
is supplied argument...
is ignored).
See also
Other functions for row and column operations in a flextable:
add_body()
,
add_body_row()
,
add_footer()
,
add_footer_lines()
,
add_footer_row()
,
add_header_row()
,
delete_columns()
,
delete_part()
,
delete_rows()
,
separate_header()
,
set_header_footer_df
,
set_header_labels()
Examples
library(flextable)
fun <- function(x) {
paste0(
c("min: ", "max: "),
formatC(range(x))
)
}
new_row <- list(
Sepal.Length = fun(iris$Sepal.Length),
Sepal.Width = fun(iris$Sepal.Width),
Petal.Width = fun(iris$Petal.Width),
Petal.Length = fun(iris$Petal.Length)
)
ft_1 <- flextable(data = head(iris))
ft_1 <- add_header(ft_1, values = new_row, top = FALSE)
ft_1 <- append_chunks(ft_1, part = "header", i = 2, )
ft_1 <- theme_booktabs(ft_1, bold_header = TRUE)
ft_1 <- align(ft_1, align = "center", part = "all")
ft_1
Sepal.Length
Sepal.Width
Petal.Length
Petal.Width
Species
min: 4.3
min: 2
min: 1
min: 0.1
max: 7.9
max: 4.4
max: 6.9
max: 2.5
5.1
3.5
1.4
0.2
setosa
4.9
3.0
1.4
0.2
setosa
4.7
3.2
1.3
0.2
setosa
4.6
3.1
1.5
0.2
setosa
5.0
3.6
1.4
0.2
setosa
5.4
3.9
1.7
0.4
setosa