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?)
kable("html")
?), and any whitespace I tried to insert just got washed away (collapsed to nothing). – Ebneter