kableExtra::pack_rows() - 分组不适用于长表中的最后一行,并且包装文本没有缩进 [英] kableExtra::pack_rows() - grouping not working for last row in long table and no indentation for wrapped text

查看:47
本文介绍了kableExtra::pack_rows() - 分组不适用于长表中的最后一行,并且包装文本没有缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 kableExtra 包在一个 R markdown 文档中生成一个长表,并编成 PDF.

使用kableExtra::pack_rows()(以前的kableExtra::group_rows())

将表的行集组合在一起

出现两个问题:

  1. 如果最后一行形成一个集合,如果有一个分组,则不显示其分组

    • 设置longtable = TRUE 允许长表跨越多个页面

      同时

    • 使用 kable_styling(..., latex_options = c("repeat_header")) 在每一页上重复表头.

  2. 我的第一列的某些行中的文本对于列宽(需要修复)来说太长了.因此它被包装到下一行.但是,任何换行的文本不再缩进.增加列宽,不幸的是,在我的用例中,文本不是一个选项.

我尝试了

longtable = TRUE AND latex_options = c("repeat_header")) ==> Pack-rows 不工作

---标题:MWE"输出:pdf_document: 默认html_document: 默认---```{r echo=FALSE}图书馆(knitr)图书馆(kableExtra)dt <- mtcars[1:3, 1:2]kable(dt,escape = FALSE, # 能够包含乳胶命令书单 = T,longtable = TRUE, # 允许长表跨越多页)%>%kable_styling(full_width = F,Latex_options = c("repeat_header" # 重复长表的表头))%>%# 列特征:设置宽度column_spec(1, width = "2cm") %>%# 将行分组并给这些组标签pack_rows("马自达汽车", 1, 2) %>%pack_rows("Datsun 汽车", 3, 3)``

解决方案

我没有足够的声誉来发表评论,所以我不得不把它作为答案,即使它只是你第二部分的部分答案问题.有一种方法可以使用 indent=FALSE 删除 pack_rows() 函数中的所有缩进:

kable(dt,escape = FALSE, # 能够包含乳胶命令书单 = T,longtable = TRUE, # 允许长表跨越多页)%>%kable_styling(full_width = F,Latex_options = c(repeat_header"# 重复长表的表头))%>%# 列特征:设置宽度column_spec(1, width = "2cm") %>%# 将行分组并给这些组标签pack_rows(马自达汽车", 1, 2, indent=FALSE) %>%pack_rows(Datsun Cars", 3, 3, indent=FALSE)

不幸的是,我的表很长,如果我能找到一种方法来缩进长行名称的两个部分,那么可读性会更好.所以我仍然希望对这个问题有更好的答案.但我希望这对某人有所帮助!

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)
```

解决方案

I don't have enough reputation to comment so I'll have to leave this as an answer even though it's only a partial answer to the second part of your question. There is a way to remove all indentation in the pack_rows() function by using indent=FALSE:

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, indent=FALSE) %>%
  pack_rows("Datsun Cars", 3, 3, indent=FALSE)

Unfortunately my table is quite long and would be much more readable if I could find a way to indent both parts of my long row name. So I'm still hoping for a better answer to this question. But I hope this helps someone!

这篇关于kableExtra::pack_rows() - 分组不适用于长表中的最后一行,并且包装文本没有缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆