'RMarkdown kable vertically align cells

I am authoring a report using RMarkdown, and using kable and kableExtra to format and print the table. Here is what I want would look like the table to look like (made in Word):

Middle Cell Alignment

I am struggling to sort the vertical centering (the space between "Number of Incident Occurences" on the top and bottom, so that the text is aligned in the middle of the row, left on the column.

Note, I am creating a PDF report and not html.

Here is a minimal example:

df <- data.frame(a = letters[1:5], b = 1:5)
names(df) <- c("A very very very very very very very very very very very very very very very very very very long title",
               "A short title")

df %>% kable(format = 'latex', linesep = "", align = 'c', escape = F) %>% kable_styling(full_width = T)

This what that output looks like:

enter image description here

The vertical alignment seems to be pushing to the top. I came across "m", but that just middle aligns the columns. I'm wondering if I need to specify something special is row_spec() for all rows for align = "m", but when I do that, it complains.... Any ideas?



Solution 1:[1]

There doesn't seem to be a way to do this directly within kable and kableExtra. However, as when building a table to PDF through kable, it uses LaTeX to build the result. We can therefore integrate LaTeX functions directly into the table.

This solution uses the multirow package. The cell to be centered vertically can be wrapped in \\multirow{1}{*}[0pt]{Cell content}, as follows:

---
header-includes:
  - \usepackage{multirow}
output: pdf_document
---

```{r}
library(knitr)
library(kableExtra)

df <- data.frame(a = letters[1:5], b = 1:5)
names(df) <- c("A very very very very very very very very very very very very very very very very very very long title",
               "\\multirow{1}{*}[0pt]{A short title}")


df %>% kable(format = 'latex', linesep = "", align = 'c', escape = F) %>% kable_styling(full_width = T)
```

enter image description here

To make using this easier, you could make a function to do the renaming for you:

centerText <- function(text){
    paste0("\\multirow{1}{*}[0pt]{", text, "}")
  }

So to rename a column you run: centerText("A short title")

Solution 2:[2]

This can be easily achieved in HTML by adding the extra_css argument. (for those who are searching for a similar solution in HTML and are only finding this post)

column_spec(1,extra_css = "vertical- align:middle;")

Solution 3:[3]

As of August 2020, The column_spec() function in the kableExtra package has a latex_valign argument. This only works if you also specify the column width, so it's not compatible with kable_styling(full_width = T)

library(knitr)
library(kableExtra)

df <- data.frame(a = letters[1:5], b = 1:5)
names(df) <- c("A very very very very very very very very very very very very very very very very very very long title",
               "A short title")

df %>% 
  kable(format = 'latex', 
        linesep = "", 
        align = 'c') %>% 
  column_spec(1:2,
              width = "3in",
              latex_valign = "m")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 B. Stanley
Solution 3 even_of_the_hour