Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Given a flextable without a header how can I add a header?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading