【问题标题】:Vertically align column name using kable and rendering with rmarkdown into html使用 kable 垂直对齐列名并使用 rmarkdown 渲染到 html
【发布时间】:2020-05-14 10:18:23
【问题描述】:

创建了下表,并希望在单元格的中心/中间垂直对齐单词“物种”。我正在使用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`

【问题讨论】:

  • 这能回答你的问题吗? RMarkdown kable vertically align cells
  • @M——遗憾的是没有。需要使用 HTML 格式进行编辑,而不是 LateX。
  • 还有第二个答案:stackoverflow.com/a/56597846/6461462
  • kable( iris , format = "html" , col.names = c( "Sepal&lt;br&gt;Length" , "Sepal&lt;br&gt;Width" , "Petal&lt;br&gt;Length" , "Petal&lt;br&gt;Width" , "Species" ) , align = "c" , escape = F ) %&gt;% kable_styling(full_width = F , bootstrap_options = c( 'hover', 'condensed' , 'bordered'), position = 'left') %&gt;% add_header_above( c('Kendal Iris Test' = 5) , bold = TRUE , background = '#0077c8' , color = 'white' ) %&gt;% kable_styling(full_width = T) %&gt;% row_spec(0 , bold = T, extra_css = 'vertical-align: middle !important;')

标签: html r r-markdown knitr kableextra


【解决方案1】:

您可以使用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;')

【讨论】:

    猜你喜欢
    • 2018-08-15
    • 2018-07-11
    • 2021-12-08
    • 2019-08-16
    • 2019-04-24
    • 1970-01-01
    • 2020-05-27
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多