How to specify table stripe colors for knitr::kable?

二次信任 提交于 2019-12-11 08:13:50

问题


How was this person able to make the tables shown in this link? It doesn't look like they were passing any arguments to kable.

Here is the example:


回答1:


The styles of kable tables are controlled by CSS file. tbody can be used to change the colour of the content of the table, with thead able to change the header.

As shown by Lee S, you can create an external CSS file. However, you can also include the CSS directly within the R Markdown file, as markdown accepts raw HTML and passes it through unaltered. See here for some more details

Here is a full reproducible example:

---
output: html_document
---

# Test Project

<style>
   tbody tr:nth-child(odd){
    background-color: #F7FBFF;
  }
</style>


```{r}
knitr::kable(mtcars[1:5, 1:5])
```

This guide provides a good explanation of the table elements which can be controlled by CSS.




回答2:


Download the RMD file http://www.reed.edu/data-at-reed/software/R/blogposts/tables_blogpost.Rmd

Change beginning of RMD file to this:

---
output: 
  html_document:
    keep_md: true
    css: mystyles.css
---

Create a css file called mystyles.css with following contents:

tbody tr:nth-child(odd){
  background-color: #F7FBFF;
}

Save to the same location as your RMD file.



来源:https://stackoverflow.com/questions/49894014/how-to-specify-table-stripe-colors-for-knitrkable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!