kableExtra icon indicating copy to clipboard operation
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

Open pmgarafola opened this issue 2 years ago • 5 comments

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)

pmgarafola avatar Mar 26 '22 21:03 pmgarafola

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.

pmgarafola avatar Mar 22 '23 14:03 pmgarafola

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 avatar Dec 04 '23 09:12 dmurdoch

@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:

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"

vincentarelbundock avatar Dec 04 '23 12:12 vincentarelbundock

Thanks. I'll put in some checks that the xml searches are successful.

dmurdoch avatar Dec 04 '23 13:12 dmurdoch

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.

dmurdoch avatar Dec 04 '23 14:12 dmurdoch