If I have a flextable
without a header, how can I add one? In the example below I am setting the scenario up artificially. i.e. I would like to turn flexiris_without_head
back into flexiris_with_head
.
library(flextable) # version 0.9.2
flexiris_with_head <- flextable(iris)
flexiris_with_head
flexiris_without_head <- flexiris_with_head %>%
delete_part(part = "header")
flexiris_without_head
If anyone is interested in more context:
I am primarily working with huxtable
objects, which I, however, need to translate to flextable
objects for docx output purposes. Using huxtable::as_flextable()
leaves me without a (technical) header, which is an issue for long tables because the headers do not roll over to the next docx page anymore.
Here is one way how we could do it using add_header_row()
:
library(dplyr)
library(flextable)
# flextable
df <- flextable(head(iris))
# iris without header -> df
df_without_header <- df %>%
delete_part(part = "header")
# adding header to flextable
df_with_header <- df_without_header %>%
add_header_row(
values = colnames(iris),
top = TRUE
) %>%
hline_top(part = "header") %>%
hline_bottom(part = "header")
df_with_header