使用 kable 垂直对齐列名称并使用 rmarkdown 渲染到 html

Vertically align column name using kable and rendering with rmarkdown into html

创建了下面的 table,并希望在单元格的 center/middle 中垂直对齐单词 "Species"。我正在使用 RMarkdown 生成 HTML 而不是 LateX。有小费吗?

---
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`

您可以使用 kableExtra::row_specs 及其 extra_css 参数:

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;')