haozhu233 / kableExtra

Construct Complex Table with knitr::kable() + pipe.
https://haozhu233.github.io/kableExtra/
Other
688 stars 147 forks source link

pack_rows() does not support all valid html character entitity references #814

Open bergsmat opened 8 months ago

bergsmat commented 8 months ago

Thanks again for this great package!

In an html context, pack_rows() allows the user to inject somewhat arbitrary html markup directly into the group labels. But certain character entity references are not supported, such as the greek letter alpha: 'α'. Meanwhile, '&#945' works just fine. This is because both are defined for html but only the latter is defined for xml.

In group_rows_html(), we see

group_header_row <- read_xml(group_header_row_text, options = "COMPACT")

which should probably be

group_header_row <- read_xml(group_header_row_text, options = "COMPACT", as_html = TRUE)

because this context is restricted to html by design.

library(kableExtra)
x <- data.frame(id = 1:2, sa = 3:4)
x %>% 
  kbl(format = 'html') %>%
  pack_rows(index = c(`&#945;` = 2), escape = FALSE) %>%
  writeLines
#> <table>
#>  <thead>
#>   <tr>
#>    <th style="text-align:right;"> id </th>
#>    <th style="text-align:right;"> sa </th>
#>   </tr>
#>  </thead>
#> <tbody>
#>   <tr grouplength="2"><td colspan="2" style="border-bottom: 1px solid;"><strong>α</strong></td></tr>
#> <tr>
#>    <td style="text-align:right;padding-left: 2em;" indentlevel="1"> 1 </td>
#>    <td style="text-align:right;"> 3 </td>
#>   </tr>
#>   <tr>
#>    <td style="text-align:right;padding-left: 2em;" indentlevel="1"> 2 </td>
#>    <td style="text-align:right;"> 4 </td>
#>   </tr>
#> </tbody>
#> </table>
x %>% 
  kbl(format = 'html') %>%
  pack_rows(index = c(`&alpha;` = 2), escape = FALSE) %>%
  writeLines
#> Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, : Entity 'alpha' not defined [26]
xml2::read_xml('<p>&alpha;</p>')
#> Error in read_xml.raw(charToRaw(enc2utf8(x)), "UTF-8", ..., as_html = as_html, : Entity 'alpha' not defined [26]
xml2::read_xml('<p>&alpha;</p>', as_html = TRUE)
#> {html_document}
#> <html>
#> [1] <body><p>α</p></body>

Created on 2024-02-05 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R Under development (unstable) (2024-02-02 r85855 ucrt) #> os Windows 10 x64 (build 19044) #> system x86_64, mingw32 #> ui RTerm #> language (EN) #> collate English_United States.utf8 #> ctype English_United States.utf8 #> tz America/New_York #> date 2024-02-05 #> pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.4.0) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.4.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.4.0) #> evaluate 0.21 2023-05-05 [1] CRAN (R 4.4.0) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.4.0) #> fs 1.6.2 2023-04-25 [1] CRAN (R 4.4.0) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.4.0) #> highr 0.10 2022-12-22 [1] CRAN (R 4.4.0) #> htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.4.0) #> kableExtra * 1.4.0 2024-01-24 [1] CRAN (R 4.4.0) #> knitr 1.42 2023-01-25 [1] CRAN (R 4.4.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.4.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.4.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.4.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.4.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.4.0) #> rlang 1.1.0 2023-03-14 [1] CRAN (R 4.4.0) #> rmarkdown 2.21 2023-03-26 [1] CRAN (R 4.4.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.4.0) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.4.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.4.0) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.4.0) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.4.0) #> svglite 2.1.1 2023-01-10 [1] CRAN (R 4.4.0) #> systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.4.0) #> viridisLite 0.4.2 2023-05-02 [1] CRAN (R 4.4.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.4.0) #> xfun 0.39 2023-04-20 [1] CRAN (R 4.4.0) #> xml2 1.3.5 2023-07-06 [1] CRAN (R 4.4.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.4.0) #> #> [1] C:/Users/tim.bergsma/AppData/Local/R/win-library/4.4 #> [2] C:/Program Files/R/R-devel/library #> #> ────────────────────────────────────────────────────────────────────────────── ```