GegznaV
GegznaV

Reputation: 5580

The contents of kableExtra table invisible if RStudio editor theme is dark

I use a dark RStudio theme and try to print a knitrExtra table in R Markdown document. Unfortunately, the main contents of the table are invisible (i.e., white symbols on white background).

Question: How to make kableExtra table contents visible in R Markdown documents with dark RStudio editor themes?

Example of code:

```{r}
library(kableExtra)

head(iris) %>% 
  knitr::kable(caption = "**Table 1.** Iris data. ", digits = 2) %>% 
  kableExtra::kable_styling()
```

An example of output:

enter image description here

If one selects the text, the contents temporarily get visible, but this is not the solution I am searching for:

enter image description here


RStudio version: 1.1.463
kabeExtra version: 0.9.0

Upvotes: 9

Views: 1210

Answers (1)

the-mad-statter
the-mad-statter

Reputation: 8711

Editing the RStudio theme file does not work because those changes get ignored when using {kableExtra} as pointed out by @Simbamangu.

Here is a work around where we edit the kable html during the print to include an inline css that styles the color.

First run this edited version of kableExtra:::print.kableExtra():

print.kableExtra <- function (x, ...) {
  view_html <- getOption("kableExtra_view_html", TRUE)
  if (view_html & interactive()) {
    dep <- list(
      rmarkdown::html_dependency_jquery(), 
      rmarkdown::html_dependency_bootstrap(theme = "cosmo"), 
      kableExtra::html_dependency_kePrint(), 
      kableExtra::html_dependency_lightable()
    )
    
    x <- sub('style="', 'style="color: black; ', as.character(x), fixed = TRUE)
        
    html_kable <- htmltools::browsable(
      htmltools::HTML(
        as.character(x), 
        "<script type=\"text/x-mathjax-config\">MathJax.Hub.Config({tex2jax: {inlineMath: [[\"$\",\"$\"]]}})</script><script async src=\"https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML\"></script>"
      )
    )
    htmltools::htmlDependencies(html_kable) <- dep
    class(html_kable) <- "shiny.tag.list"
    print(html_kable)
  }
  else {
    cat(as.character(x))
  }
}

The changes consisted of adding the x <- sub('style="', 'style="color: black; ', as.character(x), fixed = TRUE) line and also adding full references to some of the functions.

Then you can print the table as before:

head(iris) %>% 
  knitr::kable(caption = "**Table 1.** Iris data. ", digits = 2) %>% 
  kableExtra::kable_styling()

enter image description here

Upvotes: 1

Related Questions