【问题标题】:Changing the style of one table in Rmarkdown with kable使用 kable 更改 Rmarkdown 中一张表的样式
【发布时间】:2014-12-04 13:25:28
【问题描述】:

我正在使用 knitr 和 rmarkdown 编写一个小插图,选择 rmarkdown::html_vignette 样式。 我的大多数表都是作为降价管道表输入的,但我使用kable() 作为一个。

通常我喜欢表格的默认样式,但在一个特定的表格中(在几个表格中)我想抑制线条的奇偶线阴影。

有没有简单的方法来否决 CSS

table thead, table tr.even {
  background-color: #f7f7f7;
}

只针对一个特定的表,由kable 生成?

这是一个示例文件,两个表格都带有阴影。我只想要一个:

---
output: rmarkdown::html_vignette
---

This table should have alternate shading:
```{r output="asis"}
library(knitr)
kable(matrix(1:20, nrow=5))
```

How do I turn it off for just this one?
```{r output="asis"}
kable(matrix(1:20, nrow=5))
```

【问题讨论】:

    标签: knitr r-markdown


    【解决方案1】:

    这是问题的部分答案。它很丑;我希望其他人能做出更好的贡献。

    一种解决方案是为特定的 CSS id 或类定义新样式,然后将所需的表包装在 <DIV id="newid"> </DIV><DIV class="newclass"> </DIV> 中。据我所知,在对kable() 的调用中无法做到这一点,因此需要将其直接放入文本中。

    样式本身似乎也需要放入文本中,在 <STYLE></STYLE> 块中。虽然标题允许你指定css,但据我所知只能替换现有的样式文件,不能添加。

    所以这是我丑陋的解决方案:

    ---
    output: 
      rmarkdown::html_vignette
    ---
    
    This table should have alternate shading:
    ```{r output="asis"}
    library(knitr)
    kable(matrix(1:20, nrow=5))
    ```
    
    Define a style for class "nostripes", then wrap tables in a DIV with 
    class "nostripes" to turn it off:
    
    
    <style>
    .nostripes tr.even {background-color: white;}
    </style>
    <div class="nostripes">
    ```{r output="asis"}
    kable(matrix(1:20, nrow=5))
    ```
    </div>
    

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 2019-07-07
      • 2021-06-05
      • 1970-01-01
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2019-04-24
      相关资源
      最近更新 更多