I am trying to include line breaks in a table plotted using kableExtra. Adding a linebreak in add_header_above works fine using "\n"; however, adding a linebreak in column names or row names does not work (I tried to do it as described here: https://cran.r-project.org/web/packages/kableExtra/vignettes/best_practice_for_newline_in_latex_table.pdf). I also failed to include a linebreak in a normal table cell (not included in the script below). The result looks as displayed in the screenshot. I am sure that am I doing somewrong wrong here, so maybe someone can point to my mistake. Thank you in advance.
knitr::opts_chunk$set(echo = TRUE)
library(magrittr)
#create table to be plotted later
t<-data.frame(vars = 1:9, n = c(15, 15, 15, 15, 15, 15, 15,
15, 15), mean = c(2.53333333333333, 2.53333333333333, 2.53333333333333,
2.53333333333333, 2.53333333333333, 2.53333333333333, 2.53333333333333,
2.53333333333333, 2.53333333333333), sd = c(0.990430401872025,
0.990430401872025, 0.990430401872025, 0.990430401872025, 0.990430401872025,
0.990430401872025, 0.990430401872025, 0.990430401872025, 0.990430401872025
), median = c(3, 3, 3, 3, 3, 3, 3, 3, 3))
row.names(t)<- c("Teil1_1", "Teil1_2", "Teil1_3", "Teil1_4", "Teil1_5", "Teil1_6", "Teil1_7",
"Teil1_8", "Teil1_9")
#linebreak in rownames (doesn't work)
r<-row.names(t)
r[[1]]<-kableExtra::linebreak("Teil1\n_1")
row.names(t)<-r
#table with linebreak in header (doesn't work) and header above (works)
n<-names(t)
#v<-kableExtra::linebreak("Var-\niable")
#v<-"Var-\\\\iable"
v<-"Var-\niable"
#v<-"Var"
n<-append(v, n)
kableExtra::kbl(t, digits = 2, align = "c", col.names = n, escape = T, booktabs = T) %>%
kableExtra::column_spec(column = 2:ncol(t)+1, width= "5em") %>%
kableExtra::column_spec(column = 1, width= "15em") %>%
kableExtra::add_header_above(header = c("Var" = 1, "Sta\nts" = 5))