flextable
flextable copied to clipboard
Header column disappearing if merged vertically and horizontally
Salut David,
When you have a complex header with redundancy, and you want vertically and horizontally merge, some columns sometimes plainly disappear.
For instance, consider this example:
library(tibble)
library(flextable)
x = head(iris, 1) %>%
flextable()
header_mapping = tibble(col_keys=names(iris),
col1 = c("X", "X", "Y", "Y", "C"),
col2 = c("Y", "X", "A", "B", "C"))
x %>%
set_header_df(header_mapping, key = "col_keys") %>%
merge_h(part="header") %>%
merge_v(part="header") %>%
flextable::theme_box()

Created on 2022-09-09 with reprex v2.0.2
Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#> setting value
#> version R version 4.2.1 (2022-06-23 ucrt)
#> os Windows 10 x64 (build 19043)
#> system x86_64, mingw32
#> ui RTerm
#> language en
#> collate French_France.utf8
#> ctype French_France.utf8
#> tz Europe/Paris
#> date 2022-09-09
#> pandoc 2.18 @ C:/Program Files/RStudio/bin/quarto/bin/tools/ (via rmarkdown)
#>
#> ─ Packages ───────────────────────────────────────────────────────────────────
#> package * version date (UTC) lib source
#> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.2.0)
#> callr 3.7.1 2022-07-13 [1] CRAN (R 4.2.1)
#> cli 3.3.0 2022-04-25 [1] CRAN (R 4.2.1)
#> data.table 1.14.2 2021-09-27 [1] CRAN (R 4.2.0)
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.2.0)
#> evaluate 0.16 2022-08-09 [1] CRAN (R 4.2.1)
#> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0)
#> flextable * 0.7.3 2022-08-09 [1] CRAN (R 4.2.1)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0)
#> gdtools 0.2.4 2022-02-14 [1] CRAN (R 4.2.0)
#> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.2.0)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.2.0)
#> jsonlite 1.8.0 2022-02-22 [1] CRAN (R 4.2.0)
#> knitr 1.39 2022-04-26 [1] CRAN (R 4.2.0)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.2.0)
#> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0)
#> officer 0.4.3 2022-06-12 [1] CRAN (R 4.2.1)
#> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.1)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0)
#> processx 3.7.0 2022-07-07 [1] CRAN (R 4.2.1)
#> ps 1.7.0 2022-04-23 [1] CRAN (R 4.2.0)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.2.0)
#> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.2.0)
#> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.2.0)
#> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.2.0)
#> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.2.0)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0)
#> Rcpp 1.0.9 2022-07-08 [1] CRAN (R 4.2.1)
#> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1)
#> rlang 1.0.5 2022-08-31 [1] CRAN (R 4.2.1)
#> rmarkdown 2.15 2022-08-16 [1] CRAN (R 4.2.1)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.2.0)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0)
#> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.1)
#> stringr 1.4.1 2022-08-20 [1] CRAN (R 4.2.1)
#> styler 1.7.0 2022-03-13 [1] CRAN (R 4.2.0)
#> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.2.0)
#> tibble * 3.1.8 2022-07-22 [1] CRAN (R 4.2.1)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0)
#> uuid 1.1-0 2022-04-19 [1] CRAN (R 4.2.0)
#> vctrs 0.4.1 2022-04-13 [1] CRAN (R 4.2.0)
#> webshot 0.5.3 2022-04-14 [1] CRAN (R 4.2.0)
#> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0)
#> xfun 0.31 2022-05-10 [1] CRAN (R 4.2.0)
#> xml2 1.3.3 2021-11-30 [1] CRAN (R 4.2.0)
#> yaml 2.3.5 2022-02-21 [1] CRAN (R 4.2.0)
#> zip 2.2.0 2021-05-31 [1] CRAN (R 4.2.0)
#>
#> [1] C:/Users/Dan/AppData/Local/R/win-library/4.2
#> [2] C:/Program Files/R/R-4.2.1/library
#>
#> ──────────────────────────────────────────────────────────────────────────────
If you remove either one of the merge_() calls, the output will have no problem.
However, if you include both, the HTML table will be incomplete as on the image.
Indeed, in the HTML code, the second <tr> will 3 <td> but they miss the colspan=2 attribute
It seems that each merge_() call is overriding the previous one.
When submitting a new issue:
-
[x] Provide the code that is producing the error, it has to be a minimal reproducible example. Stackoverflow is providing good explanations about it: https://stackoverflow.com/help/mcve. You can use package
reprexto help you: http://reprex.tidyverse.org/. The most popular R stackoverflow question is about the subject: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. -
[x] Provide the results of R command
sessionInfo(). It had to be executed after you loaded the packages used by your example. This will let me know what is your version of R and what are the versions of the packages you used in your example. -
[x] Make sure you did checked you had the latest version of the package on CRAN (and on github if issue exists with CRAN version).
-
[x] Make sure you searched in the open and closed issues on the github repository.
Hello Dan,
I am not sure it's possible what you are trying to do.
Let's start from here:
library(tibble)
library(flextable)
x = head(iris, 1) %>%
flextable()
header_mapping = tibble(col_keys=names(iris),
col1 = c("X", "X", "Y", "Y", "Y"),
col2 = c("Y", "X", "A", "B", "Y"))
x %>%
set_header_df(header_mapping, key = "col_keys") %>%
flextable::theme_box() %>%
print(preview = "docx")
Now in Word (because it let us try to do it manually),
- try to merge x[1, 2:4], this works,
- then try to merge x[1:2, 4] - I can't make it work!
Can you confirm this is what you are trying to do?
Actually, this is just a bug report that columns are disappearing.
I updated my original example so it is more clear. I expect column C to be merged, but A and B should not be shifted to the left.
Sometimes you want both vertical and horizontal merges at different places. I would expect some kind of check so that any merge would abort if one cell is already merged.
Of course, a workaround is to specify the rows and columns you want to merge.
Thanks for your answer Dan, I understand now