kableExtra::pack_rows() - grouping not working for last row in long table and no indentation for wrapped text

断了今生、忘了曾经 提交于 2020-07-03 05:25:11

问题


I use the kableExtra package to generate a long table in an R markdown documents knitted to PDF.

Sets of rows of the table are grouped together using kableExtra::pack_rows() (formerly kableExtra::group_rows())

Two issues arise:

  1. If the last row forms a set, its grouping is not displayed if one

    • sets longtable = TRUE to allow the long table to span across multiple pages

      and at the same time

    • uses kable_styling(..., latex_options = c("repeat_header")) to repeat the header of the table on each page.

  2. The text in some rows of my first column is too long for the column width (which needs to be fixed). It is thus wrapped into the next line. However, any wrapped text is no longer indented. Increasing the column width such that the text is unfortunately not an option in my use case.

I tried the minipage solution proposed in this SO post to solve issue 2). It works if the first column is set sufficiently wide via column_spec(1, width = "XXXem")). However, in my use case, I can not set it soooo wide as to make it work this way.

If there is no other to indent the content of the wrapped line, could one alternatively remove the indentation in general?

An MWE and illustrations are provided below.

Either longtable = FALSE or no latex_options = c("repeat_header")) ==> Pack-rows is working

longtable = TRUE AND latex_options = c("repeat_header")) ==> Pack-rows is not working

---
title: "MWE"
output:
  pdf_document: default
  html_document: default
---
```{r echo=FALSE}
library(knitr)
library(kableExtra)

dt <- mtcars[1:3, 1:2]

kable(dt, 
  escape = FALSE, # to be able to include latex commands
  booktabs = T, 
  longtable = TRUE, # allow long table to span multiple pages
  ) %>% 
  kable_styling(full_width = F,
                latex_options = c(
                  "repeat_header"  # repeat header of long table
                )
  ) %>% 
  # column characteristics: set widths
  column_spec(1, width = "2cm") %>%
  # group rows together and give these groups labels
  pack_rows("Mazda Cars", 1, 2) %>%
  pack_rows("Datsun Cars", 3, 3)
```

来源:https://stackoverflow.com/questions/62454558/kableextrapack-rows-grouping-not-working-for-last-row-in-long-table-and-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!