Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle kable object that includes extra stuff #743

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/kableExtra-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#' @importFrom stringr fixed str_count str_split str_match str_detect str_match_all
#' str_extract str_replace_all str_trim str_extract_all str_sub str_replace
#' @importFrom xml2 read_xml xml_attr xml_has_attr xml_attr<- read_html
#' xml_child xml_children xml_name xml_add_sibling xml_add_child xml_text
#' xml_child xml_children xml_name xml_add_sibling xml_add_child xml_text xml_length
#' xml_remove write_xml xml_text<- xml_length
#' @importFrom rvest html_table
#' @importFrom knitr knit_meta_add include_graphics knit_print asis_output kable
Expand Down
5 changes: 3 additions & 2 deletions R/kable_styling.R
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ htmlTable_styling <- function(kable_input,
}
kable_attrs <- attributes(kable_input)
kable_xml <- read_kable_as_xml(kable_input)

pre <- attr(kable_xml, "pre")
post <- attr(kable_xml, "post")
# Modify class
bootstrap_options <- match.arg(
bootstrap_options,
Expand Down Expand Up @@ -296,7 +297,7 @@ htmlTable_styling <- function(kable_input,
}
}

out <- as_kable_xml(kable_xml)
out <- as_kable_xml(kable_xml, pre, post)
if (protect_latex) {
out <- replace_latex_in_kable(out, kable_attrs$extracted_latex)
kable_attrs$extracted_latex <- NULL
Expand Down
37 changes: 34 additions & 3 deletions R/util.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,45 @@ regex_escape <- function(x, double_backslash = FALSE) {
return(x)
}

as_kable_xml <- function(x) {
out <- structure(as.character(x), format = "html", class = "knitr_kable")
as_kable_xml <- function(x, pre = NULL, post = NULL) {
out <- structure(paste(c(pre, as.character(x), post), collapse = "\n"),
format = "html", class = "knitr_kable")
return(out)
}

is_html_table <- function(x) {
xml_length(x) == 1 &&
xml_name(xml_child(x, 1)) == "table"
}

child_to_character <- function(x) {
result <- character()
n <- xml_length(x)
for (i in seq_len(n)) {
if (xml_name(xml_child(x, i)) != "meta")
result <- c(result, as.character(xml_child(x, i)))
}
result
}

read_kable_as_xml <- function(x) {
kable_html <- read_html(as.character(x), options = c("RECOVER", "NOERROR"))
xml_child(xml_child(kable_html, 1), 1)
children <- lapply(seq_len(xml_length(kable_html)),
function(num) xml_child(kable_html, num))
pre <- character(0)
post <- character(0)
result <- list()
for (i in seq_along(children)) {
if (!is_html_table(children[[i]]))
pre <- c(pre, child_to_character(children[[i]]))
else {
result <- xml_child(children[[i]], 1)
for (j in seq_along(children)[-seq_len(i)])
post <- c(post, child_to_character(children[[i]]))
break;
}
}
structure(result, pre = pre, post = post)
}

#' LaTeX Packages
Expand Down