kableExtra
kableExtra copied to clipboard
Error in UseMethod("nodeset_apply") : no applicable method for 'nodeset_apply' applied to an object of class "NULL" when trying to print side by side tables
Describe the bug
When I use kableExtra to print a subset of a tibble as a side-by-side table, I get the error message:
Error in UseMethod("nodeset_apply") : no applicable method for 'nodeset_apply' applied to an object of class "NULL"
The problem is the row_spec specification. When I remove this it works fine (except I don't get the desired row spec).
This only happens when I execute the R Markdown chunk from within R Studio. If I knit the document to PDF it works fine and produces the expected output. I am including the YAML header and the set up chunk for reproducibility.
R Version: 4.1.2, R Studio Version: 2922.02.1 Build 461, kableExtra Version: 1.34.9000
To Reproduce
---
title: kable extra issue
date: "`r Sys.Date()`"
output:
pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = TRUE,
fig.align = "center",
fig.height = 5,
fig.path = "./figures/",
fig.width = 7,
message = FALSE,
warning = FALSE,
comment = NA
)
rm(list=ls())
#
library(tidyverse)
library(lubridate)
library(knitr)
library(kableExtra)
#
```
```{r}
testTab <- tibble(COL1 = str_c("Column 1 Row",seq(1,50)),
COL2 = str_c("Column 2 Row",seq(1,50)),
COL3 = str_c("Column 3 Row",seq(1,50)))
#
kable(list(testTab[1:10,],testTab[11:20,]),
booktabs=TRUE,linesep="",align=c("l","r","r"),
caption="Test Table - First 20 Rows Side bby Side")%>%
kable_styling(latex_options = c("striped","hold_position"),
font_size=8) %>% row_spec(0,bold=T)
#
```
title: "A reproducible example in rmarkdown" output: html_document
library(kableExtra)
If you leave out the "%>% row_spec" if works fine. Something with specifying a row_spec when placing two tables side by side does not work.
When you are in RStudio, kable()
will default to HTML output. I get the same error in the document if I change the output format to html_document
. You can avoid the problem in RStudio by specifying format = "latex"
explicitly in the call.
It is probably a bug that you get the error you saw with HTML output, but it doesn't really make sense to specify latex_options
for HTML output. So I won't track this down, but I'll leave the issue open if someone else wants to take a look.
@dmurdoch
Below I paste two simpler examples of the problem. The problem seems to be that none of these tables have a thead
component. Internally, we call:
kable_thead <- xml_tpart(kable_xml, "thead")
which returns NULL
. Then we call
xml_children(kable_thead)
which errors. This happens in at least two places:
- https://github.com/haozhu233/kableExtra/blob/master/R/row_spec.R#L92
- https://github.com/haozhu233/kableExtra/blob/master/R/column_spec.R#L163
Simple examples:
library(kableExtra)
kbl(list(mtcars[1:2, 1:2], mtcars[3:4, 3:4]), format = "html") |>
column_spec(1, include_thead = TRUE)
# Error in UseMethod("nodeset_apply"): no applicable method for 'nodeset_apply' applied to an object of class "NULL"
kbl(list(mtcars[1:2, 1:2], mtcars[3:4, 3:4]), format = "html") |>
row_spec(0)
# Error in UseMethod("nodeset_apply"): no applicable method for 'nodeset_apply' applied to an object of class "NULL"
Thanks. I'll put in some checks that the xml searches are successful.
I've added checks, but haven't merged the PR because HTML and PDF behaviour is inconsistent, and I don't know which is correct.