Color trouble with Rmarkdown generated HTML tables

74 Views Asked by At

Maybe this is just a stupid browser related issue, but ...

I generate some colored tables in a HTML file via Rmarkdown using the tableHTML package (colors are generated by the RColorBrewer package):

```{r Init, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(magrittr, quietly = TRUE)
require(tableHTML)
test <- data.frame(A=123, B=456, C=789)
```

...

```{r Test, echo=FALSE, results="asis"}
tableHTML(test) %>%
  add_css_thead(css = list('background-color', '#4DAF4A')) %>%
  add_css_table(css = list('border', '#4DAF4A'))
```

The colors of the borders look fine in the `Rstudio' viewer:

RStudio viewer

But the borders are colored different, when opening in Safari or Firefox:

Safari Firefox

What is the problem with table border colors?

2

There are 2 best solutions below

2
On BEST ANSWER

Thank you very much for your question!

With add_css_table, the style is applied to the table-tag and with add_css-thead, it is applied to the thead tag. But I think to get the effect you are looking for, you can apply the styles to the th and td tags via the functions add_css_header and add_css_column:

tableHTML(test) %>%
    add_css_header(css = list(c("background-color", "border"),
                              c("#4DAF4A", "1px solid #4DAF4A")),
                   headers = 0:4) %>% 
    add_css_column(css = list("border", "1px solid #4DAF4A"),
                   columns = c("rownames", names(test)))

In RStudio, it looks like this:

table_rstudio

In Firefox:

table_firefox

And in Chrome:

table_chrome

2
On

According to https://www.colorhexa.com/4daf4a the closest websafe colour to "#4daf4a" is "#669933". Does changing the border to #669933 solve your problem?