From 32c30203ca6bf6908ba67b86e84052b425e86c04 Mon Sep 17 00:00:00 2001 From: Duncan Murdoch Date: Wed, 31 Jan 2024 15:33:16 -0500 Subject: [PATCH] Get data even if there is no table header. --- DESCRIPTION | 2 +- R/util.R | 22 +++++++++++++++++----- inst/NEWS.md | 9 +++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index e22850d..922080d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 = 'haozhu233@gmail.com', role = c('aut', 'cre'), comment = c(ORCID = '0000-0002-3386-6076')), diff --git a/R/util.R b/R/util.R index bf56b92..779dd7c 100644 --- a/R/util.R +++ b/R/util.R @@ -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) { diff --git a/inst/NEWS.md b/inst/NEWS.md index 34dc11f..980f999 100644 --- a/inst/NEWS.md +++ b/inst/NEWS.md @@ -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 --------------------------------------------------------------------------------