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

Conversation

dmurdoch
Copy link
Collaborator

@dmurdoch dmurdoch commented Feb 8, 2023

My tables package has a toKable function so that it can produce
output to be modified by kableExtra. I just noticed that it isn't
working any more, and I've narrowed down the issue to the following kind
of example:

  tables::table_options(doCSS = TRUE)
  tab <- tables::tabular(mpg ~ mean, data = mtcars)
  kab <- tables::toKable(tab, format = "html")
  cat(as.character(kab))
#> <style>
#> .Rtable thead, .Rtable .even {
#>   background-color: inherit;
#> }
#> .left   { text-align:left; }
#> .center { text-align:center; }
#> .right  { text-align:right; }
#> .Rtable, .Rtable thead {
#>   border-collapse: collapse;
#>   border-style: solid;
#>   border-width: medium 0;
#>   border-color: inherit;
#> }
#> .Rtable th, .Rtable td {
#>   padding-left: 0.5em;
#>   padding-right: 0.5em;
#>   border-width: 0;
#> }
#> </style>
#>  <table class="Rtable">
#>  <thead>
#>  <tr class="center">
#>   <th></th>
#>   <th>mean</th>
#> </tr>
#>  </thead>
#>  <tbody>
#>  <tr class="center">
#>   <th class="left">mpg</th>
#>   <td>20.09</td>
#> </tr>
#>  </tbody>
#>  </table>
  kab2 <- kab |> kableExtra::kable_styling(full_width = FALSE)
  cat(as.character(kab2))
#> <style class="table" style="width: auto !important; margin-left: 
auto; margin-right: auto;">
#> .Rtable thead, .Rtable .even {
#>   background-color: inherit;
#> }
#> .left   { text-align:left; }
#> .center { text-align:center; }
#> .right  { text-align:right; }
#> .Rtable, .Rtable thead {
#>   border-collapse: collapse;
#>   border-style: solid;
#>   border-width: medium 0;
#>   border-color: inherit;
#> }
#> .Rtable th, .Rtable td {
#>   padding-left: 0.5em;
#>   padding-right: 0.5em;
#>   border-width: 0;
#> }
#> </style>

If you look at the HTML code in kab, it's a <style> ... </style> block
followed by a <table> ... </table> block, but in kab2, the table is dropped. This happens when htmlTable_styling converts its input to XML: it assumes that the input is always just one block containing the table.

The current PR drops this assumption. It looks through the XML for the first table, and just copies everything earlier or later into the final htmlTable_styling output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants