kableExtra
kableExtra copied to clipboard
In R< 3.4, collapse_rows fails
Problem:
When collapsing rows in an R version less than 3.4, the collapse_row()
function fails. THe min R version of kableExtra needs to be updated, or subsetting updated in this function.
This is likely due to the fact that {rvest} now returns tibbles, not data.frames. It seems this is the issue as I traced this down:
- in
collapse_rows_html()
, rvest is used to createkable_dt
to get the original data.frame. thencollapse_row_matrix()
function calls -
kable_dt
is fed intocollapse_row_matrix()
, which then loop over the columns identified incollapse_rows()
to collapse at with callrle(kable_dt[,i])
, wherei
is one of the columns. - in new R versions, but in R < 3.4 you get:
Error in matrix(unlist(value, recursive = FALSE, use.names = FALSE), nrow = nr, :
'data' must be of a vector type, was 'NULL'
this works post >= R 3.4 is because of a change in behavior in the ==
/!=
functions.
REPREX:
library(knitr)
library(kableExtra)
data.frame(x = c(1,1,1,2,2,2), y = c(1:6)) %>%
kable() %>%
collapse_rows(1)
Resolution:
I think this could be resolved by updating this line and any other cases that rely on rle to :
rle(kable_dt[[ i]])
now it will be expressly converted into a vector no matter the tibble/data.frame or R version.
library(tibble)
t_cars <- tibble(mtcars)
## success
rle(mtcars[,1])
## failure
rle(t_cars[,1])
## success
rle(mtcars[[1]])
## success
rle(t_cars[[1]])