Row indentation with add_indent() in 2nd column in kableExtra
Asked Answered
H

1

6

I try to indent cells of the 2nd column of a dataframe using the kableExtra-package for RMarkdown. It seems add_indent() only works for the first column, therefore does not change anything in my table of the reprex below:

Reprex with dummy data:

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

Desired output: I would like to indent the rows 2, 3, 5, 6 of the 2nd column (that the answer options are intended below the questions and ideally also in italics). Italics could also be covered with cell_spec() but that works just column wise I think.

Is my desired output possible? (I guess it does not make sense to mix questions & answer options, but to keep up the format of an earlier report we would like to try it that way?)

Hemline answered 26/2, 2019 at 16:56 Comment(0)
Y
4

Here are two possible ways.

  1. Use kableExtra::group_rows isntead of an extra group column.

  2. Add the indentation (kableExtra adds 1em) manually using cell_spec.

Option 1

```{r, echo = F}
df <- data.frame(quest, percent) 
df %>%
  mutate(quest = cell_spec(quest, italic = ifelse(row_number() %in% c(2,3,5,6), T, F))) %>%
  kable(booktabs = T, escape = F) %>%
  add_indent(c(2,3,5,6)) %>% 
  group_rows("Group 1", 1, 3) %>%
  group_rows("Group 2", 4, 6) %>%
  gsub("NA", " ", .)
```

Option 2

```{r, echo = F}
df <- data.frame(group, quest, percent)
df %>%
  mutate(quest = cell_spec(quest, italic = ifelse(row_number() %in% c(2,3,5,6), T, F)),
         quest = ifelse(row_number() %in% c(2,3,5,6), paste0("\\hspace{1em}", quest), quest)) %>%
  kable(booktabs = T, escape = F) %>%
  gsub("NA", " ", .)
``` 

enter image description here

Yellowstone answered 27/2, 2019 at 0:28 Comment(2)
Option 1 was very easy. I tried option 2, but it didn't work (perhaps because I was using kable("html")?), and any whitespace I tried to insert just got washed away (collapsed to nothing).Ebneter
Yes, the approach works with pdf only.Yellowstone

© 2022 - 2024 — McMap. All rights reserved.