在kableExtra的第二列中使用add_indent()行缩进

问题描述 投票:0回答:1

我尝试使用RMarkdown的kableExtra-package缩进数据帧第二列的单元格。似乎add_indent()只适用于第一列,因此不会改变我在下面的reprex表中的任何内容:

具有虚拟数据的Reprex:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(tidyverse)
library(kableExtra)


group <- c(1, NA, NA, 2, NA, NA)
quest <- c("How is your mood today?", "good or very good", "bad or very bad", 
           "What colour is your hair?", "brown", "other")
percent <- c(NA, 80, 20, NA, 50, 50)


df <- tibble(group, quest, percent)


```

## Reprex

```{r, echo=TRUE}
# output without add_indent()
kable(df, booktabs = T, escape = T) %>% 
  add_header_above(header = c("Group" = 1, 
                              "Question & answer options" = 1, 
                              " %Agreement" = 1)) %>% 
  gsub("NA", " ", .)
```


```{r with indent, echo=TRUE}
# output with add_indent()
kable(df, booktabs = T, escape = T) %>% 
  add_header_above(header = c("Group" = 1, 
                              "Question & answer options" = 1, 
                              " %Agreement" = 1)) %>% 
  gsub("NA", " ", .) %>% 
  add_indent(positions = c(2,3,5,6))

期望的输出:我想缩进第2列的第2,3,5,6行(答案选项是在问题的下方,理想情况下也是斜体)。斜体也可以用cell_spec()覆盖,但我认为这只是列式的。

我想要的输出可能吗? (我想混合问题和答案选项是没有意义的,但为了保持早期报告的格式我们想以这种方式尝试吗?)

r r-markdown kableextra
1个回答
1
投票

这有两种可能的方法。

  1. 使用qazxsw poi而不是额外的qazxsw poi列。
  2. 使用qazxsw poi手动添加缩进(qazxsw poi ads qazxsw poi)。

选项1

kableExtra::group_rows

group

© www.soinside.com 2019 - 2024. All rights reserved.