kableExtra icon indicating copy to clipboard operation
kableExtra copied to clipboard

pack_rows() creates an indentation in table produced with kable in rmarkdown

Open MMotaFerreira opened this issue 10 months ago • 1 comments

I'm trying to create a relatively complex table with kable in rmarkdown, where I need to pack_rows() with collapse_rows() and add_header_above().

When I use pack_rows(), kable creates an indentation in the middle of the table:

library(tidyverse)
library(kableExtra)

Catch_Ratio_Meshsize <- structure(list(Lake_Code = c("RK_20", "RK_20", "RK_20", "RK_20", "RO_21", "RO_21", "SC_18", "SC_18", "SC_18", "SC_18"), 
                                       Zone = c("Benthic", "Benthic", "Pelagic", "Pelagic", "Benthic", "Benthic", "Benthic", "Benthic", "Pelagic", "Pelagic"), 
                                       Net_Type = c("DIN", "MOD", "DIN", "MOD", "DIN", "MOD", "DIN", "MOD", "DIN", "MOD"), 
                                       `5` = c(0.0227848101265823, 0, 0, 0, 0.0113285272914521, 0, 0, 0, 0, 0), 
                                       `6.3` = c(0.164556962025316, 0.0506329113924051, 0, 0, 0.043254376930999, 0.0350154479917611, 0.0221843003412969, 0.00170648464163823, 0.0050251256281407, 0), 
                                       `8` = c(0.154430379746835, 0.0506329113924051, 0, 0, 0.0154479917610711, 0.0185375901132853, 0.0793515358361775, 0.0290102389078498, 0.0301507537688442, 0.0201005025125628), 
                                       `10` = c(0.126582278481013, 0.0607594936708861, 0.2, 1.6, 0.200823892893924, 0.0514933058702369, 0.175767918088737, 0.0614334470989761, 0.0402010050251256, 0), 
                                       `12.5` = c(0.134177215189873, 0.126582278481013, 0.2, 0.9, 0.199794026776519, 0.0844490216271885, 0.34641638225256, 0.133105802047782, 0.391959798994975, 0.195979899497487), 
                                       `15.5` = c(0.151898734177215, 0.134177215189873, 0.4, 0.9, 0.208032955715757, 0.185375901132853, 0.215017064846416, 0.199658703071672, 0.381909547738693, 0.417085427135678), 
                                       `19.5` = c(0.126582278481013, 0.0936708860759494, 0, 0, 0.167868177136972, 0.189495365602472, 0.0998293515358362, 0.083617747440273, 0.125628140703518, 0.0251256281407035),
                                       `24` = c(0.0860759493670886, 0.0329113924050633, 0.1, 0.2, 0.0978372811534501, 0.0865087538619979, 0.0324232081911263, 0.0409556313993174, 0.0251256281407035, 0.0703517587939699), 
                                       `29` = c(0.010126582278481, 0.0278481012658228, 0.1, 0, 0.0247167868177137, 0.0638516992790937, 0.0110921501706485, 0.0119453924914676, 0, 0),
                                       `35` = c(0.0177215189873418, 0.020253164556962, 0, 0, 0.0185375901132853, 0.03913491246138, 0.0119453924914676, 0.0426621160409556, 0, 0), 
                                       `43` = c(0.00506329113924051, 0.00253164556962025, 0, 0, 0.0113285272914521, 0.0164778578784758, 0.00255972696245734, 0.0273037542662116, 0, 0), 
                                       `55` = c(0, 0, 0, 0, 0.00102986611740474, 0, 0.00341296928327645, 0, 0, 0), 
                                       total = c(1, 0.6, 1, 3.6, 1, 0.770339855818744, 1, 0.631399317406143, 1, 0.728643216080402), 
                                       Lake_Year = c("Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrköpfle - 2020", "Rohrsee - 2021", "Rohrsee - 2021", "Schluchsee - 2018", "Schluchsee - 2018", "Schluchsee - 2018", "Schluchsee - 2018" )), class = "data.frame", row.names = c(NA, -10L))


Catch_Ratio_Meshsize %>% 
  select(-c(Lake_Code, Lake_Year)) %>% 
  rename("\U00A0" = "Zone") %>% 
  kbl(digits = 3) %>%
  kable_classic("striped", full_width = T) %>% 
  column_spec(15, bold = Catch_Ratio_Meshsize$total > 1) %>% 
  collapse_rows(columns = 1, valign = "top") %>%
  pack_rows(index = table(Catch_Ratio_Meshsize$Lake_Year), indent = F, label_row_css = "border-bottom: 1px solid;") %>%
  add_header_above(c(" " = 2, "Mesh size" = 12, " " = 1), line = TRUE, 
                   extra_css = "border-bottom: 1px solid;") 

Table with unwanted indentation

Someone already suggest to me to update packages. I was able to run it in two computers. One is a regular Win10 PC with a somewhat old version. Another an M1 Mac with the latest version of the softwares and packages. Still the same result.

Here are the sessionInfo() results: Win10

sessionInfo()

R version 4.1.3 (2022-03-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_Europe.1252  LC_CTYPE=English_Europe.1252    LC_MONETARY=English_Europe.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Europe.1252    

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

other attached packages:
 [1] kableExtra_1.4.0 forcats_0.5.2    stringr_1.5.1    dplyr_1.1.2      purrr_1.0.1      readr_2.1.3      tidyr_1.3.0      tibble_3.2.1    
 [9] ggplot2_3.4.2    tidyverse_1.3.2 

loaded via a namespace (and not attached):
 [1] fs_1.5.2            usethis_2.1.6       lubridate_1.9.0     devtools_2.4.5      httr_1.4.4          bslib_0.4.1         tools_4.1.3        
 [8] profvis_0.3.7       backports_1.4.1     utf8_1.2.3          R6_2.5.1            DBI_1.1.3           colorspace_2.1-0    urlchecker_1.0.1   
[15] withr_2.5.0         tidyselect_1.2.0    prettyunits_1.2.0   processx_3.8.0      compiler_4.1.3      cli_3.6.1           rvest_1.0.3        
[22] xml2_1.3.3          sass_0.4.5          scales_1.2.1        callr_3.7.3         systemfonts_1.0.4   digest_0.6.30       rmarkdown_2.18     
[29] svglite_2.1.1       pkgconfig_2.0.3     htmltools_0.5.4     sessioninfo_1.2.2   dbplyr_2.2.1        fastmap_1.1.0       highr_0.10         
[36] htmlwidgets_1.6.1   rlang_1.1.0         readxl_1.4.1        rstudioapi_0.14     shiny_1.7.4         jquerylib_0.1.4     generics_0.1.3     
[43] jsonlite_1.8.4      googlesheets4_1.0.1 magrittr_2.0.3      Rcpp_1.0.9          munsell_0.5.0       fansi_1.0.4         lifecycle_1.0.4    
[50] stringi_1.7.6       pkgbuild_1.4.0      grid_4.1.3          promises_1.2.0.1    crayon_1.5.2        miniUI_0.1.1.1      haven_2.5.1        
[57] hms_1.1.3           knitr_1.40          ps_1.7.2            pillar_1.9.0        pkgload_1.3.1       reprex_2.0.2        glue_1.6.2         
[64] evaluate_0.23       remotes_2.4.2       modelr_0.1.10       vctrs_0.6.1         tzdb_0.3.0          httpuv_1.6.9        cellranger_1.1.0   
[71] gtable_0.3.3        assertthat_0.2.1    cachem_1.0.6        xfun_0.34           mime_0.12           xtable_1.8-4        broom_1.0.1        
[78] later_1.3.0         googledrive_2.0.0   viridisLite_0.4.2   gargle_1.2.1        memoise_2.0.1       timechange_0.1.1    ellipsis_0.3.2     

Mac M1:

sessionInfo()

R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.4.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Berlin
tzcode source: internal

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

other attached packages:
 [1] kableExtra_1.4.0 lubridate_1.9.3  forcats_1.0.0    stringr_1.5.1   
 [5] dplyr_1.1.4      purrr_1.0.2      readr_2.1.5      tidyr_1.3.1     
 [9] tibble_3.2.1     ggplot2_3.5.0    tidyverse_2.0.0 

loaded via a namespace (and not attached):
 [1] sass_0.4.9        utf8_1.2.4        generics_0.1.3    xml2_1.3.6       
 [5] stringi_1.8.3     hms_1.1.3         digest_0.6.35     magrittr_2.0.3   
 [9] evaluate_0.23     grid_4.3.3        timechange_0.3.0  fastmap_1.1.1    
[13] jsonlite_1.8.8    ggeffects_1.5.2   fansi_1.0.6       viridisLite_0.4.2
[17] scales_1.3.0      jquerylib_0.1.4   abind_1.4-5       cli_3.6.2        
[21] rlang_1.1.3       munsell_0.5.1     cachem_1.0.8      withr_3.0.0      
[25] yaml_2.3.8        tools_4.3.3       tzdb_0.4.0        colorspace_2.1-0 
[29] vctrs_0.6.5       R6_2.5.1          lifecycle_1.0.4   car_3.1-2        
[33] insight_0.19.10   pkgconfig_2.0.3   bslib_0.7.0       pillar_1.9.0     
[37] gtable_0.3.5      glue_1.7.0        systemfonts_1.0.6 xfun_0.43        
[41] tidyselect_1.2.1  highr_0.10        rstudioapi_0.16.0 knitr_1.45       
[45] htmltools_0.5.8.1 rmarkdown_2.26    carData_3.0-5     svglite_2.1.3    
[49] compiler_4.3.3  

MMotaFerreira avatar Apr 23 '24 10:04 MMotaFerreira