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

collapse_rows example from docs fails #624

Closed
RPanczak opened this issue Jun 7, 2021 · 7 comments
Closed

collapse_rows example from docs fails #624

RPanczak opened this issue Jun 7, 2021 · 7 comments

Comments

@RPanczak
Copy link

RPanczak commented Jun 7, 2021

I'm trying to implement collapse_rows example from docs. It fails to give me correct output.

collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
                               C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
                               C3 = 1:15,
                               C4 = sample(c(0,1), 15, replace = TRUE))
kbl(collapse_rows_dt, align = "c") %>%
  kable_paper(full_width = F) %>%
  column_spec(1, bold = T) %>%
  collapse_rows(columns = 1:2, valign = "top")

Produces

image

Instead

image

My sessionInfo():

R version 4.0.4 (2021-02-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
[1] kableExtra_1.3.4

loaded via a namespace (and not attached):
 [1] compiler_4.0.4    pillar_1.6.1      bslib_0.2.5.1    
 [4] jquerylib_0.1.4   highr_0.9         tools_4.0.4      
 [7] digest_0.6.27     jsonlite_1.7.2    evaluate_0.14    
[10] lifecycle_1.0.0   tibble_3.1.2      viridisLite_0.4.0
[13] pkgconfig_2.0.3   rlang_0.4.11      rstudioapi_0.13  
[16] xfun_0.23         httr_1.4.2        stringr_1.4.0    
[19] xml2_1.3.2        knitr_1.33        vctrs_0.3.8      
[22] sass_0.4.0        systemfonts_1.0.2 webshot_0.5.2    
[25] svglite_2.0.0     glue_1.4.2        R6_2.5.0         
[28] fansi_0.5.0       rmarkdown_2.8     magrittr_2.0.1   
[31] scales_1.1.1      htmltools_0.5.1.1 ellipsis_0.3.2   
[34] rvest_1.0.0       colorspace_2.0-1  utf8_1.2.1       
[37] stringi_1.6.2     munsell_0.5.0     crayon_1.4.1    
@florisvdh
Copy link

Exactly the same here! collapse_rows() seems to not alter its input.

This appears to be a duplicate of #596. I've added some findings there.

@askarlupka
Copy link

It's not working for me either. There was a previous issue, #595 which is closed now. But still after installing the ref # "a6af5c0", it was still not working

@jacpete
Copy link

jacpete commented Aug 26, 2021

I can confirm that this is still an issue on the version currently available on CRAN (1.3.4). I was having the same issues that led me here and looking to make a bug fix, but I think you just need to check the installation version of the package and ensure the bug fix in a6af5c0 is implemented.

install.packages('kableExtra')

library(kableExtra)
# Warning message:
# package ‘kableExtra’ was built under R version 4.0.5 

sessionInfo()
# R version 4.0.4 (2021-02-15)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 17763)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] kableExtra_1.3.4
# 
# loaded via a namespace (and not attached):
# [1] rstudioapi_0.13   knitr_1.33        xml2_1.3.2        magrittr_2.0.1    rvest_1.0.0       munsell_0.5.0     viridisLite_0.4.0
# [8] colorspace_2.0-1  R6_2.5.0          rlang_0.4.11      stringr_1.4.0     httr_1.4.2        tools_4.0.4       webshot_0.5.2    
# [15] xfun_0.23         htmltools_0.5.1.1 systemfonts_1.0.2 gitcreds_0.1.1    digest_0.6.27     lifecycle_1.0.0   glue_1.4.2       
# [22] evaluate_0.14     rmarkdown_2.8     stringi_1.6.2     compiler_4.0.4    scales_1.1.1      svglite_2.0.0  

kableExtra:::collapse_rows_html
# function (kable_input, columns, valign, target) 
# {
#     kable_attrs <- attributes(kable_input)
#     kable_xml <- kable_as_xml(kable_input)
#     kable_tbody <- xml_tpart(kable_xml, "tbody")
#     kable_dt <- rvest::html_table(xml2::read_html(as.character(kable_input)))[[1]]
#     if (is.null(columns)) {
#         columns <- seq(1, ncol(kable_dt))
#     }
#     if (!is.null(target)) {
#         if (!target %in% columns) {
#             stop("target has to be within the range of columns")
#         }
#     }
#     if (!is.null(kable_attrs$header_above)) {
#         kable_dt_col_names <- unlist(kable_dt[kable_attrs$header_above, 
#         ])
#         kable_dt <- kable_dt[-(1:kable_attrs$header_above), ]
#         names(kable_dt) <- kable_dt_col_names
#     }
#     collapse_matrix <- collapse_row_matrix(kable_dt, columns, 
#                                            target = target)
#     for (i in 1:nrow(collapse_matrix)) {
#         matrix_row <- collapse_matrix[i, ]
#         names(matrix_row) <- names(collapse_matrix)
#         target_row <- xml_child(kable_tbody, i)
#         row_node_rm_count <- 0
#         for (j in 1:length(matrix_row)) {
#             collapsing_col <- as.numeric(sub("x", "", 
#                                              names(matrix_row)[j])) - row_node_rm_count
#             target_cell <- xml_child(target_row, collapsing_col)
#             if (matrix_row[j] == 0) {
#                 xml_remove(target_cell)
#                 row_node_rm_count <- row_node_rm_count + 1
#             }
#             else if (matrix_row[j] != 1) {
#                 xml_attr(target_cell, "rowspan") <- matrix_row[j]
#                 xml_attr(target_cell, "style") <- paste0(xml_attr(target_cell, 
#                                                                   "style"), "vertical-align: ", valign, 
#                                                          " !important;")
#             }
#         }
#     }
#     out <- as_kable_xml(kable_xml)
#     kable_attrs$collapse_matrix <- collapse_matrix
#     attributes(out) <- kable_attrs
#     if (!"kableExtra" %in% class(out)) 
#         class(out) <- c("kableExtra", class(out))
#     return(out)
# }
# <bytecode: 0x0000019f13ac1550>
# <environment: namespace:kableExtra>

The CRAN function is missing the fix from a6af5c0 .
When I run the example code, I get the wrong output.

collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
                               C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
                               C3 = 1:15,
                               C4 = sample(c(0,1), 15, replace = TRUE))
kbl(collapse_rows_dt, align = "c") %>%
    kable_paper(full_width = F) %>%
    column_spec(1, bold = T) %>%
    collapse_rows(columns = 1:2, valign = "top")

However after installing the package with the commit from GitHub as mentioned here, it does work.

remotes::install_github('haozhu233/kableExtra@a6af5c0')
library(kableExtra)
sessionInfo()
# R version 4.0.4 (2021-02-15)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 17763)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] kableExtra_1.3.4.9000
# 
# loaded via a namespace (and not attached):
# [1] compiler_4.0.4    prettyunits_1.1.1 remotes_2.4.0     tools_4.0.4       digest_0.6.27     pkgbuild_1.2.0    evaluate_0.14    
# [8] lifecycle_1.0.0   viridisLite_0.4.0 rlang_0.4.11      cli_2.5.0         rstudioapi_0.13   curl_4.3.2        xfun_0.23        
# [15] withr_2.4.2       httr_1.4.2        stringr_1.4.0     knitr_1.33        xml2_1.3.2        gitcreds_0.1.1    systemfonts_1.0.2
# [22] rprojroot_2.0.2   webshot_0.5.2     svglite_2.0.0     glue_1.4.2        R6_2.5.0          processx_3.5.2    rmarkdown_2.8    
# [29] callr_3.7.0       magrittr_2.0.1    scales_1.1.1      ps_1.6.0          htmltools_0.5.1.1 rvest_1.0.0       colorspace_2.0-1 

collapse_rows_dt <- data.frame(C1 = c(rep("a", 10), rep("b", 5)),
                               C2 = c(rep("c", 7), rep("d", 3), rep("c", 2), rep("d", 3)),
                               C3 = 1:15,
                               C4 = sample(c(0,1), 15, replace = TRUE))
kbl(collapse_rows_dt, align = "c") %>%
    kable_paper(full_width = F) %>%
    column_spec(1, bold = T) %>%
    collapse_rows(columns = 1:2, valign = "top")

If you are still having issues with the development version of the package, I think the next step may be to look at the other dependencies this function relies on. {rvest} was mentioned as the cause of the change that required the bug fix in the first place. You may check the installation version of those packages. mine are:

library(rvest)
library(xml2)
sessionInfo()
# R version 4.0.4 (2021-02-15)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 17763)
# 
# Matrix products: default
# 
# locale:
# [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
# [4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    
# 
# attached base packages:
# [1] stats     graphics  grDevices utils     datasets  methods   base     
# 
# other attached packages:
# [1] xml2_1.3.2  rvest_1.0.0
# 
# loaded via a namespace (and not attached):
# [1] httr_1.4.2      compiler_4.0.4  magrittr_2.0.1  R6_2.5.0        gitcreds_0.1.1  tools_4.0.4     lifecycle_1.0.0 rlang_0.4.11 

@florisvdh
Copy link

Confirmed, thanks! It works fine with current GitHub version installed with remotes::install_github('haozhu233/kableExtra'). (Currently at commit 4c93f1a)

@florisvdh
Copy link

So this issue could be closed, or perhaps rephrased to say that the CRAN version still misses the fix.

@paulrougieux
Copy link

The current CRAN version kableExtra_1.3.4 still doesn't work for html output, the pdf version works however.

@jkylearmstrong
Copy link

Confirmed, thanks! It works fine with current GitHub version installed with remotes::install_github('haozhu233/kableExtra'). (Currently at commit 4c93f1a)

Confirmed that this is working for HTML with remotes::install_github('haozhu233/kableExtra') - however, the CRAN version is still not working.

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

No branches or pull requests

7 participants