kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

In R< 3.4, collapse_rows fails

Open thebioengineer opened this issue 3 years ago • 1 comments

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 create kable_dt to get the original data.frame. thencollapse_row_matrix() function calls
  • kable_dt is fed into collapse_row_matrix(), which then loop over the columns identified in collapse_rows() to collapse at with call rle(kable_dt[,i]), where i 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.

thebioengineer avatar Apr 19 '21 18:04 thebioengineer

library(tibble)

t_cars <- tibble(mtcars)

## success
rle(mtcars[,1])

## failure
rle(t_cars[,1])

## success
rle(mtcars[[1]])

## success
rle(t_cars[[1]])

thebioengineer avatar Apr 19 '21 19:04 thebioengineer