2

I am trying to use two variables to group data: Country and State_Province. I want to use 'flextable' in R to do this and I also want the two variables to be next to each other in the resulting table separated by a vertical bar.

However using merge_v seems to get rid of the vertical bar and I can't figure out how to add it back in.

Here is a small reproducible example:

library(flextable)
library(dplyr)
big_border = fp_border(color="#2c3a51" , width = 2)
std_border = fp_border(color="#2c3a51" , width = 1)

df <- data.frame(Country = c("US", "US", "US", "Canada", "Canada", "Canada"), 
                 State_Province = c("CA", "CA", "NY", "Quebec", "Quebec", "Alberta"), 
                 data = LETTERS[1:6])

df %>% 
  flextable()  %>% 
  vline( border = std_border, part = "all" ) %>% 
  border_outer(part="all", border = big_border ) %>% 
  merge_v(j = c("Country", "State_Province") )  

Flextable with vertical bar missing between two groups used to merge cell

Instead I have been adding a serperator and removing it before presentations.

myft <- df %>% 
  flextable(., col_keys = c("Country", "separator", "State_Province", "data")) 
myft <- border(myft, j = ~ separator, border = fp_border(width=0), part = "all")
myft <- width(myft, j = ~ separator, width = 0)


myft %>% 
  merge_v(j = c("Country") )  %>% 
  vline( border = std_border, part = "all" ) %>% 
  border_outer(part="all", border = big_border ) %>% 
  merge_v(j = c("State_Province") )  

enter image description here

  • could you give a try with the latest version (>=0.5.0) - I think it is solved as I can not reproduce it. – David Gohel Jan 31 '19 at 16:51
  • 1
    I updated R to 3.5.2 and flextable 0.5.0. It exports to pptx, word, and html fine. The default R studio viewer seems to have trouble though. It looses most of the color formatting in R studio notebooks as well when knitting just that chunk . – Lionel Duarte Jan 31 '19 at 18:10
  • I think you mean the default Windows RStudio viewer. The rendering is supported for officer and R Markdown; I am not using RS notebooks, I should give a try. – David Gohel Feb 01 '19 at 07:48
  • Had the same problem in RStudio viewer in Windows. When printing it to a docx everything was perfect. Borders with width less than 1 are not shown in RStudio viewer, too. – TobiSonne May 14 '20 at 11:38

0 Answers0