RMarkdown kable vertically align cells
Asked Answered
S

3

9

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?

Story answered 7/3, 2018 at 3:36 Comment(5)
Try and be as concise as possible: it makes it easier to help. I've edited the question and also included a reproducible example.Triplicity
Possible duplicate of Vertical align kable's column nameTriplicity
@MikeyHarper - Thank you for the edit, I will keep this in mind for future posts.Story
Great, I always prefer editing posts to show how they could be improved. Also, you may want to check out this: rprogramming.net/rename-columns-in-r In your original code you were renaming all the columns separately which is a long-winded way of doing it. Just rename them all with a listTriplicity
@MikeyHarper - I did lots of searching for this both in SO and in Google, and never saw that post. Thanks for the example - looks like the same thing to me. Not sure how I never came across that one in my 2 days of searching.Story
T
7

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

Triplicity answered 8/3, 2018 at 23:24 Comment(7)
Ahh, so if I take all the content of the cells and just do a paste0() on them to wrap them in this function, then everything should center up ok.... Very neat. I knew I could do this in general, I just wasn't sure what package or Latex function to use. Very cool. Thank you!Story
Yeah, I just added a function which would do the pasting for youTriplicity
You were on my same line of thinking.... already coding up a function. Thought about just doing using paste0() to replace the contents of the cell, but a function sounded better..... Good call.Story
Some updates - the solution above does work, but only if you do not need to have kable "sanitize" your table contents. In my case, I do, so when I use the "escape = TRUE" option, I actually get the "\multicolumn...." printed out. If I use "escape = FALSE", then I get a whole slew of other issues since the text in the table didn't get sanitized. I tried sanitizing the text first (using the xtable sanitize function) then using the function above to add the multicolumn. That worked, but all of my column formatting (widths and such) were gone since multicolumn had the {*} called out.Story
To correct this I added a couple conditions to determine column width, since this will be fixed in my case. However, now the column height is an issue. Seems like there is no simple way to do this.... Or maybe I am making it too complicated.....Story
This does not work with latex_engine: xelatex; with an error "! You can't use `\relax' after \the."Adverse
"\\multirow{1}{*}[0pt]{A short title}" doesn't work work for me but "\\multirow{2}{*}[0pt]{A short title}" does.Sackey
S
12

This can be 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;")
Shortcircuit answered 14/6, 2019 at 12:4 Comment(2)
Thanks very much! I was searching how to vertically align the text to the top of the cells in HTML tables, and came across your solution - worked like a charm! Thank you! :)Hargett
This worked great after I realized the space in "vertical- align:middle;" shouldn't be there. I used extra_css = "vertical-align:middle;", instead.Klingensmith
T
7

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

Triplicity answered 8/3, 2018 at 23:24 Comment(7)
Ahh, so if I take all the content of the cells and just do a paste0() on them to wrap them in this function, then everything should center up ok.... Very neat. I knew I could do this in general, I just wasn't sure what package or Latex function to use. Very cool. Thank you!Story
Yeah, I just added a function which would do the pasting for youTriplicity
You were on my same line of thinking.... already coding up a function. Thought about just doing using paste0() to replace the contents of the cell, but a function sounded better..... Good call.Story
Some updates - the solution above does work, but only if you do not need to have kable "sanitize" your table contents. In my case, I do, so when I use the "escape = TRUE" option, I actually get the "\multicolumn...." printed out. If I use "escape = FALSE", then I get a whole slew of other issues since the text in the table didn't get sanitized. I tried sanitizing the text first (using the xtable sanitize function) then using the function above to add the multicolumn. That worked, but all of my column formatting (widths and such) were gone since multicolumn had the {*} called out.Story
To correct this I added a couple conditions to determine column width, since this will be fixed in my case. However, now the column height is an issue. Seems like there is no simple way to do this.... Or maybe I am making it too complicated.....Story
This does not work with latex_engine: xelatex; with an error "! You can't use `\relax' after \the."Adverse
"\\multirow{1}{*}[0pt]{A short title}" doesn't work work for me but "\\multirow{2}{*}[0pt]{A short title}" does.Sackey
F
5

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")
Faro answered 19/4, 2022 at 19:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.