Skip to content

Commit

Permalink
Merge pull request #813 from haozhu233/issue812
Browse files Browse the repository at this point in the history
Get data even if there is no table header.
  • Loading branch information
haozhu233 authored Jan 31, 2024
2 parents 9779f52 + 32c3020 commit b1bae80
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: kableExtra
Type: Package
Title: Construct Complex Table with 'kable' and Pipe Syntax
Version: 1.4.0
Version: 1.4.0.1
Authors@R: c(
person('Hao', 'Zhu', email = '[email protected]', role = c('aut', 'cre'),
comment = c(ORCID = '0000-0002-3386-6076')),
Expand Down
22 changes: 17 additions & 5 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,25 @@ get_xml_text <- function(xml_node) {
read_table_data_from_xml <- function(kable_xml) {
thead <- xml_tpart(kable_xml, "thead")
tbody <- xml_tpart(kable_xml, "tbody")
if (is.null(tbody))
stop("table has no body!")

# Header part
n_header_rows <- xml2::xml_length(thead)
col_headers_xml <- xml2::xml_children(xml2::xml_child(thead, n_header_rows))
col_headers <- unlist(lapply(col_headers_xml, get_xml_text))
n_cols <- length(col_headers)
first_column_as_row_names <- (col_headers[1] == '')
if (!is.null(thead)) {
n_header_rows <- xml2::xml_length(thead)
col_headers_xml <- xml2::xml_children(xml2::xml_child(thead, n_header_rows))
col_headers <- unlist(lapply(col_headers_xml, get_xml_text))
n_cols <- length(col_headers)
first_column_as_row_names <- (col_headers[1] == '')
} else {
first_column_as_row_names <- FALSE
col_headers <- NULL
# We have no header, so get the maximum number of columns in the body
n_cols <- vapply(xml2::xml_children(tbody),
function(row) length(xml2::xml_children(row)),
1L)
n_cols <- max(n_cols)
}

# Content part
filtered_rows <- lapply(xml2::xml_children(tbody), function(row) {
Expand Down
9 changes: 9 additions & 0 deletions inst/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
kableExtra 1.4.0.1
--------------------------------------------------------------------------------

Bug Fixes:

* Fixed a bug in `collapse_rows()`, which failed on tables
that had no header (#812).


kableExtra 1.4.0
--------------------------------------------------------------------------------

Expand Down

0 comments on commit b1bae80

Please sign in to comment.