rr-markdownflextableofficerofficedown

Missing outer border of a flextable in R


I have an issue I can't resolve. There may be something missing in the flextable package, but I may be wrong. Please, see the example. This is my tabular data:

structure(list(groupName = c("Group A", "Group A", "Group A", 
"Group A", "Group A", "Group B", "Group B", "Group B", "Group B", 
"Group B"), ID = c("14243", "14363", "14373", "14934", "14980", 
"14400", "14488", "14513", "14549", "15407"), `Day -15` = c(NA, 
NA, NA, 174, 143, NA, NA, NA, NA, 169), `Day -13` = c(NA, NA, 
NA, 164, 132, NA, NA, NA, NA, 178), `Day -12` = c(NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_), `Day -11` = c(NA, NA, NA, NA, 189, NA, 213, NA, NA, 
204), `Day -10` = c(NA, NA, NA, 187, NA, NA, NA, NA, NA, NA), 
    `Day -9` = c(NA, NA, NA, NA, 178, NA, 204, 227, 257, 232), 
    `Day -8` = c(NA, NA, NA, 286, NA, NA, NA, NA, NA, NA), `Day -7` = c(NA, 
    NA, NA, NA, NA, NA, 258, 311, 291, NA)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

I build a flextable like this:

flx <- flextable(tbl2) %>% 
  border_outer() %>% 
  add_header_row(values = c(
    "groupName",
    "ID",
    rep("measurement", ncol(tbl2) - 2)
  )) %>% 
  merge_h(part = "header") %>% 
  merge_v(part = "header") %>% 
  valign(valign = "bottom", part = "header") %>% 
  align(align = "center", part = "all")

and the output is enter image description here As You can see there is missing border in the first row of the header. I tried to add border_outer() once again, but no effect. I also tried vline_right function, but no effect too. The only thing I found was to add this step:

flx %>% border_inner_v(part = "header")

but it adds border to every cell in the header which is not what I need. enter image description here

I think the step merge_h in my pipeline is the place where bad things happen. Is there a solution for this?


Solution

  • This is a special case, the border you are seeing on the right side is actually the right border of the third cell that has been merged with all following cells.

    library(flextable)
    library(magrittr)
    
    tbl2 <- structure(list(groupName = c("Group A", "Group A", "Group A", 
    "Group A", "Group A", "Group B", "Group B", "Group B", "Group B", 
    "Group B"), ID = c("14243", "14363", "14373", "14934", "14980", 
    "14400", "14488", "14513", "14549", "15407"), `Day -15` = c(NA, 
    NA, NA, 174, 143, NA, NA, NA, NA, 169), `Day -13` = c(NA, NA, 
    NA, 164, 132, NA, NA, NA, NA, 178), `Day -12` = c(NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_), `Day -11` = c(NA, NA, NA, NA, 189, NA, 213, NA, NA, 
    204), `Day -10` = c(NA, NA, NA, 187, NA, NA, NA, NA, NA, NA), 
    `Day -9` = c(NA, NA, NA, NA, 178, NA, 204, 227, 257, 232), 
    `Day -8` = c(NA, NA, NA, 286, NA, NA, NA, NA, NA, NA), `Day -7` = c(NA, 
    NA, NA, NA, NA, NA, 258, 311, 291, NA)), row.names = c(NA, -10L), 
    class = c("tbl_df", "tbl", "data.frame"))
    
    # solution 1
    flextable(tbl2) %>% 
      border_outer() %>% 
      add_header_row(values = c(
        "groupName",
        "ID",
        rep("measurement", ncol(tbl2) - 2)
      )) %>% 
      merge_h(part = "header") %>% 
      merge_v(part = "header") %>% 
      valign(valign = "bottom", part = "header") %>% 
      align(align = "center", part = "all") %>% 
      vline(i = 1, j = 3, border = fp_border_default(), part = "header")
    
    # solution 2
    flextable(tbl2) %>% 
      border_outer() %>% 
      add_header_row(values = c(
        "groupName",
        "ID",
        rep("measurement", ncol(tbl2) - 2)
      )) %>% 
      merge_h(part = "header") %>% 
      merge_v(part = "header") %>% 
      valign(valign = "bottom", part = "header") %>% 
      align(align = "center", part = "all") %>% 
      fix_border_issues()
    
    

    enter image description here