hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Borders don't work correctly with merged cells #246

Closed cwilso6 closed 1 year ago

cwilso6 commented 1 year ago

I am attempting to make tables for a Word document where I want a border above the footnote. However, I have merged cells in the last row of the actual table. The border does not extend the entire width of the table, it does not put a border on the column with the merged cells. In my example, the column with the merged cells happens to be the left-most column. I have attempted several workarounds but none of them seem to work. Below is an example:


output: word_document

knitr::opts_chunk$set(echo = FALSE)
library(huxtable)
library(tidyverse)

data = rbind(mtcars |> rownames_to_column('Car'), 
             mtcars |> rownames_to_column('Car')) |>
  mutate(Car = factor(Car)) |>
  arrange(Car) |>
  head(10) |>
  mutate(Car = as.character(Car))

Without merging cells

#Without merging cells

ht <- as_hux(data, add_colnames = T)|>
  add_footnote(text = "footer", border = 0.4) |>
  set_caption('Title') |>
  set_caption_pos(value =  'topleft')

bottom_border(ht)[1,] <- brdr(thickness = 0.4)
top_border(ht)[1,] <- brdr(thickness = 0.4)

ht

\newpage

Merging cells

ht <- as_hux(data, add_colnames = T)|>
  add_footnote(text = "footer", border = 0.4) |>
  merge_repeated_rows(col = 1) |>
  set_caption('Title') |>
  set_caption_pos(value =  'topleft')

bottom_border(ht)[1,] <- brdr(thickness = 0.4)
top_border(ht)[1,] <- brdr(thickness = 0.4)

ht

\newpage

ht <- as_hux(data, add_colnames = T)|>
  add_footnote(text = "footer", border = 0) |> #changed to border to 0
  merge_repeated_rows(col = 1) |>
  set_caption('Title') |>
  set_caption_pos(value =  'topleft')

bottom_border(ht)[1,] <- brdr(thickness = 0.4)
top_border(ht)[1,] <- brdr(thickness = 0.4)
top_border(ht)[nrow(data)+2,] <- brdr(thickness = 0.4) # the +2 is accounting for the column names and footer
top_border(ht)[nrow(data)+2,1] <- brdr(thickness = 0.4) # to attenpt to get the first column

ht
hughjonesd commented 1 year ago

Yup, this is indeed a bug and it is docx specific.

hughjonesd commented 1 year ago

Weirdly, it shows up in quick_docx but not in the internal viewer for as_flextable.

hughjonesd commented 1 year ago

which makes me think this is a bug in flextable's docx output but not its html output

hughjonesd commented 1 year ago

Yeah. So if I run flextable::print(as_flextable(ht), preview = "html") I don't get the bug, but if I use preview="docx", I do.

My feeling is this may be a flextable bug... or a subtle interaction between the systems? @davidgohel might have a clue.

hughjonesd commented 1 year ago

More generally, the bug doesn't seem to happen with flextable::save_as_image or save_as_rtf

hughjonesd commented 1 year ago

Possibly related: davidgohel/flextable#577.

hughjonesd commented 1 year ago

@cwilso6 please could you check if latest github master fixes this

cwilso6 commented 1 year ago

Thanks for all of your work on this @hughjonesd! The line after the data/before the footer does appear to span the entire table.