1

Created the table below and would like to vertically align the word "Species" in the center/middle of the cell. I am using RMarkdown to generate the HTML not LateX. Any tips?

enter image description here

---
title: "Untitled"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE )


library( kableExtra )
options( scipen = 99 )

```

```{r iris}

tab_iris <- knitr::kable( iris , format = "html" , col.names = c( "Sepal<br>Length" , "Sepal<br>Width" , "Petal<br>Length" , "Petal<br>Width" , "Species" ) , align = "c" , escape = F ) %>% kable_styling(full_width = F , bootstrap_options = c( 'hover', 'condensed' , 'bordered'), position = 'left') %>% add_header_above( c('Kendal Iris Test' = 5) , bold = TRUE , background = '#0077c8' , color = 'white' ) 

```

`r tab_iris`
M--
  • 25,431
  • 8
  • 61
  • 93
kendalorgera
  • 147
  • 2
  • 11
  • 2
    Does this answer your question? [RMarkdown kable vertically align cells](https://stackoverflow.com/questions/49143690/rmarkdown-kable-vertically-align-cells) – M-- Jan 28 '20 at 23:12
  • 1
    @M-- sadly no. Need to make the edit with HTML formatting, not LateX. – kendalorgera Jan 28 '20 at 23:33
  • There's a second answer: https://stackoverflow.com/a/56597846/6461462 – M-- Jan 28 '20 at 23:39
  • 1
    ```kable( iris , format = "html" , col.names = c( "Sepal
    Length" , "Sepal
    Width" , "Petal
    Length" , "Petal
    Width" , "Species" ) , align = "c" , escape = F ) %>% kable_styling(full_width = F , bootstrap_options = c( 'hover', 'condensed' , 'bordered'), position = 'left') %>% add_header_above( c('Kendal Iris Test' = 5) , bold = TRUE , background = '#0077c8' , color = 'white' ) %>% kable_styling(full_width = T) %>% row_spec(0 , bold = T, extra_css = 'vertical-align: middle !important;')```
    – M-- Jan 29 '20 at 00:08

1 Answers1

4

You can use kableExtra::row_specs and its extra_css argument:

library(knitr)
library(kableExtra)

kable( head(iris) , format = "html" , 
       col.names = c( "Sepal<br>Length" , "Sepal<br>Width" , 
                      "Petal<br>Length" , "Petal<br>Width" , "Species" ) , 
       align = "c" , escape = F ) %>% 
  kable_styling(full_width = F , bootstrap_options = c( 'hover', 'condensed' , 'bordered'), 
                                 position = 'left') %>% 
  add_header_above( c('Kendal Iris Test' = 5) , bold = TRUE , 
                    background = '#0077c8' , color = 'white' ) %>% 
  kable_styling(full_width = T) %>% 
  row_spec(0 ,  bold = F, extra_css = 'vertical-align: middle !important;')

M--
  • 25,431
  • 8
  • 61
  • 93