8

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?

5
  • 1
    Try and be as concise as possible: it makes it easier to help. I've edited the question and also included a reproducible example. Mar 8, 2018 at 23:18
  • 1
    Possible duplicate of Vertical align kable's column name Mar 8, 2018 at 23:33
  • 1
    @MikeyHarper - Thank you for the edit, I will keep this in mind for future posts.
    – azdatasci
    Mar 8, 2018 at 23:34
  • 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 list Mar 8, 2018 at 23:35
  • 1
    @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.
    – azdatasci
    Mar 8, 2018 at 23:35

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

8

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;")
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! :)
    – elarry
    Sep 10, 2020 at 11:23
  • 1
    This worked great after I realized the space in "vertical- align:middle;" shouldn't be there. I used extra_css = "vertical-align:middle;", instead.
    – pneud
    Jul 19, 2021 at 11:51
6

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

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!
    – azdatasci
    Mar 8, 2018 at 23:32
  • 1
    Yeah, I just added a function which would do the pasting for you Mar 8, 2018 at 23:39
  • 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.
    – azdatasci
    Mar 8, 2018 at 23:41
  • 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.
    – azdatasci
    Mar 11, 2018 at 21:57
  • 2
    "\\multirow{1}{*}[0pt]{A short title}" doesn't work work for me but "\\multirow{2}{*}[0pt]{A short title}" does. Jun 8, 2020 at 2:27
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.