Skip to content

save_as_html crash when zero body rows and deleted header #722

@hansepac

Description

@hansepac

save_as_html() (and officer::body_add_flextable()) crash when a flextable has zero body rows and the header part has been deleted via delete_part().

Error: Error in setorderv(): some columns are not in the data.table: [.row_id]

The error originates in flextable:::information_data_cell(), which builds a data.table and calls setorderv(dat, c(".part", ".row_id", ".col_id")). When there are zero rows and no header part, .row_id is never created in the internal data.table, so setorderv() errors.

Tested on both CRAN (0.9.11) and dev (0.9.12.8) versions.
Also tested with data.table 1.16.4 and 1.18.2.1 — both fail.

# Minimal reproducible example 
library(flextable)

# Works: zero rows with header intact
ft_ok <- flextable(data.frame(a = character(0), b = character(0)))
save_as_html(ft_ok, path = tempfile(fileext = ".html"))

# Fails: zero rows with header deleted
ft_bad <- flextable(data.frame(a = character(0), b = character(0))) |>
  delete_part(part = "header")
save_as_html(ft_bad, path = tempfile(fileext = ".html"))
#> Error in setorderv():
#> some columns are not in the data.table: [.row_id]

sessionInfo()

# R version 4.4.1 (2024-06-14)
# Platform: aarch64-apple-darwin20
# Running under: macOS 26.3
#
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base
#
# other attached packages:
# [1] flextable_0.9.12.008
#
# loaded via a namespace (and not attached):
#  [1] gtable_0.3.6        dplyr_1.2.0         compiler_4.4.1
#  [4] tidyselect_1.2.1    Rcpp_1.1.1          zip_2.3.3
#  [7] xml2_1.5.2          tidyr_1.3.2         fontquiver_0.2.1
# [10] scales_1.4.0        systemfonts_1.3.2   textshaping_1.0.5
# [13] uuid_1.2-2          fastmap_1.2.0       ggplot2_4.0.2
# [16] R6_2.6.1            gdtools_0.5.0       generics_0.1.4
# [19] knitr_1.51          tibble_3.3.1        openssl_2.3.5
# [22] pillar_1.11.1       rlang_1.1.7         xfun_0.57
# [25] cli_3.6.5           magrittr_2.0.4      digest_0.6.39
# [28] grid_4.4.1          lifecycle_1.0.5     vctrs_0.7.2
# [31] glue_1.8.0          data.table_1.18.2.1 officer_0.7.3
# [34] ragg_1.5.1          rmarkdown_2.30      purrr_1.2.1
# [37] tools_4.4.1         pkgconfig_2.0.3     htmltools_0.5.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions