Css RMarkdown:如何旋转表列标题

Css RMarkdown:如何旋转表列标题,css,r-markdown,Css,R Markdown,我正在尝试在RMarkdown中旋转表列标题。由于我不了解CSS,到目前为止,我的尝试被证明是徒劳的 那么,如何旋转表列标题 下面是RMarkdown中表格的一个最小示例: --- title: "Untitled" output: html_document: df_print: paged style: ./Table.css --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) libr

我正在尝试在RMarkdown中旋转表列标题。由于我不了解CSS,到目前为止,我的尝试被证明是徒劳的

那么,如何旋转表列标题

下面是RMarkdown中表格的一个最小示例:

---
title: "Untitled"
output: 
  html_document:
    df_print: paged
    style: ./Table.css
---

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

<div class="table-header-rotated">

```{r test, result="asis"}

kable(head(mtcars), "html")
```
</div>
---
标题:“无标题”
输出:
html_文件:
df_打印:第页
样式:./Table.css
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
图书馆(kableExtra)
图书馆(knitr)
```
```{r test,result=“asis”}
卡布尔(头部(mtcars),“html”)
```
CSS文件是根据此处提供的代码生成的:


有人能给我一个提示吗?

你可以从
kableExtra
使用
kable\u style()
row\u spec()

---
title: "Untitled"
output: 
  html_document:
    df_print: paged
    style: ./Table.css
---

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

<div class="table-header-rotated">

```{r test, result="asis"}
kable(head(mtcars), "html") %>%
kable_styling("striped", full_width = F) %>%
  row_spec(0, angle = -45)
```
---
标题:“无标题”
输出:
html_文件:
df_打印:第页
样式:./Table.css
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
图书馆(kableExtra)
图书馆(knitr)
```
```{r test,result=“asis”}
卡布尔(头部(mtcars),“html”)%%>%
可折叠样式(“条纹”,全宽=F)%>%
行规格(0,角度=-45)
```
收益率:

如果捕获html输出,可以使用
gsub
编辑适当的行,以匹配所需的
div
span
调用,然后使用
cat
返回编辑的输出。我无法让
css
完全按照链接上显示的方式工作,但下面采用的方法确实旋转了标签:

以“{css}”开头的第一个块


th.rotate {
  /* Something you can count on */
  height: 140px;
  white-space: nowrap;
}

th.rotate > div {
  transform: 
    /* Magic Numbers */
    translate(25px, 51px)
    /* 45 is really 360 - 45 */
    rotate(315deg);
  width: 30px;
}

th.rotate > div > span {
  border-bottom: 1px solid #ccc;
  padding: 5px 10px;
}


x%
gsub(“([^(>|)]+)”,
'\\2',x=)%>%
类别(,sep=“\n”)

谢谢,但这不适用于较大的列。插入
mtcars这也不会使列变得更窄,如果列的宽度是由于长列标题造成的,您是否找到了解决此问题的适当方法?不,不太遗憾。

x <- kable(head(mtcars), "html")

x %>% 
  gsub('<th style="text-align:(left|center|right);"> ([^(>| )]+) </th>', 
       '<th class="rotate"><div><span>\\2</span></div></th>', x = .) %>% 
  cat(., sep = "\n")