Search code examples
rflextableofficer

Row height with flextable and officer


I have a new computer (read: new versions of everything, including Office 2016). I created the following code on my previous computer, and all worked fine:

...
control_table <- regulartable(data = data) %>%
  theme_box() %>%
  rotate(rotation = "btlr", part = "header") %>%
  align(align = "left", part = "body") %>% 
  set_header_labels(Var1 = " " ) %>% 
  align(align = "left", part = "header") %>%
  height(height = 3, part = "header") %>%
  width(width = 0.3) %>%
  width(j = 1, width = 3.5) 

doc <- doc %>%
  cursor_reach("The following table indicates the reports") %>%
  body_add_flextable(control_table, align = "left")
...

Now with my new computer the row height of the header is not being translated into the Word document. dim(control_table) gives the correct row height, but the header row height is not displaying in the Word document. What am I missing?


Solution

  • Word don't handle auto height with rotated headers, then it is necessary to specify rule for row height with function hrule.

    library(flextable)
    library(officer)
    library(magrittr)
    
    control_table <- flextable(data = head(iris)) %>%
      theme_box() %>%
      rotate(rotation = "btlr", part = "header") %>%
      align(align = "left", part = "body") %>% 
      set_header_labels(Var1 = " " ) %>% 
      align(align = "left", part = "header") %>%
      height(height = 2, part = "header") %>%
      hrule(i = 1, rule = "exact", part = "header")
    
    
    doc <- read_docx() %>%
      body_add_flextable(control_table, align = "left") %>% 
      print(target = "example.docx")
    

    enter image description here