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

1

9

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

Threshold answered 11/11, 2018 at 10:36 Comment(10)
you should head over to the rstudio/rstudio github project and file an issue for this. the burden should not be on you to make this work.Binny
hrm. i just tried this and it works for me. What version of RStudio? I'm on 1.2.1049 (I use the RStudio daily builds). knitr and kableExtra versions might be good to know as well.Binny
RStudio 1.1.463 (current officially released version). When is RStudio 1.2 going to be officially released?Threshold
I have no idea. It's a pretty big change from 1.1.x and has alot of new features. Can you try the Preview edition?Binny
I tried the preview version and the contents of the table are visible there. But there are some other unsolved bugs in the preview version, e.g., it shows only a fraction of output and hides other is some situation (link). Thus I do not prefer it yet.Threshold
Unfortunately, there's no obvious way to get the inline cell previewer to use different CSS so this might be an issue until 1.2 is officially out.Binny
@hrbrmstr, please add the answer that the problem is solved in the development version of RStudio an I will accept it.Threshold
Shouldn't we make sure they come through in the final, imminent release of 1.2? :-) I mean, I 💙 those folks but they may let this slip through the cracks.Binny
I'm on rstudio Version 1.3.1093, kableExtra version 1.3.4 and am having the same issues as @Threshold was back in 2018. !GegznaV, did you find a solution?Suburbicarian
@Suburbicarian I use RStudio 1.4 and the most current versions of the packages, but the issue persists.Threshold
L
1

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

Lifetime answered 6/4, 2022 at 17:25 Comment(1)
Doesn't work with OP's example. kableExtra seems to be what's breaking this - try with the code supplied by OP, e.g. knitr::kable(head(iris)) %>% kableExtra::kable_styling()Duleba

© 2022 - 2024 — McMap. All rights reserved.