4

I'm using R Markdown in RStudio and prefer to write code using a black background. My tables are formatted nicely in the output html document using kable. However, I've noticed that I can't read the output from kable because the text color is black:

enter image description here

But if I use pander, it knows to use white text...

enter image description here

But oh man that is ugly in my report: enter image description here

How can I either get kable to use white text inside RStudio, or get pander to produce prettier tables in my report?

Nova
  • 5,423
  • 2
  • 42
  • 62
  • Not sure about defaults, but you could use `table.attr = "style=\"color: white;\""` ... – Martin Schmelzer Nov 28 '16 at 19:58
  • Not sure where you mean to put that piece of code - and will it change the color of the text inside Rstudio? I can't seem to get it to work (Markdown noob here)... – Nova Nov 28 '16 at 21:11
  • Here: `kable(head(iris), format = "html", table.attr = "style = \"color: white;\"")` If you use it very often you could create a snippet so its a little more comfy to use. – Martin Schmelzer Nov 28 '16 at 21:12
  • ok - got that to work - but now it's not showing up in my (white-backgrounded) report. Can't have it both ways? – Nova Nov 28 '16 at 21:31
  • Moved this down to the answer section. – Martin Schmelzer Nov 28 '16 at 21:37

1 Answers1

5

You could use

kable(head(iris), format = "html", table.attr = "style = \"color: white;\"")

in order to have the notebook previews use a white font color. If you want the final output to be formatted another way, just use some CSS at the beginning of your Rmarkdown:

<style>
table {
  background-color: white !important;
  color: black !important;
}
</style>

The !important rule overrides any other styles.

enter image description here

Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
  • Ok, cool. I looked around because those html type tables are kind of ugly (even in your example above the titles of the fields run together). The code here works, but I'm not sure if it can be somehow nested in the style bit you included in your answer - and I'm also not sure if it's the right way to add padding between columns (the titles end up looking a bit "off" compared to the field contents). Any further suggestions? `` – Nova Nov 29 '16 at 20:00
  • Do you want the padding to be applied to the final document or the preview chunks? – Martin Schmelzer Nov 30 '16 at 12:47
  • To the final document, in the preview chunks it looks fine. Thank you Martin for continuing to help me! – Nova Nov 30 '16 at 15:42
  • Ok, what you have to be aware of is that the table head uses `th` tags instead of `td`'s. So just add `th { padding: 0 30px 0 30px; }`to the CSS style block. – Martin Schmelzer Nov 30 '16 at 23:04