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

user438383
  • 5,716
  • 8
  • 28
  • 43
GegznaV
  • 4,938
  • 4
  • 23
  • 43
  • 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. – hrbrmstr Nov 11 '18 at 10:40
  • 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. – hrbrmstr Nov 11 '18 at 10:42
  • RStudio 1.1.463 (current officially released version). When is RStudio 1.2 going to be officially released? – GegznaV Nov 11 '18 at 11:06
  • 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? – hrbrmstr Nov 11 '18 at 12:03
  • 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](https://github.com/rstudio/rstudio/issues/3536)). Thus I do not prefer it yet. – GegznaV Nov 11 '18 at 12:22
  • 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. – hrbrmstr Nov 11 '18 at 12:28
  • @hrbrmstr, please add the answer that the problem is solved in the development version of RStudio an I will accept it. – GegznaV Nov 20 '18 at 17:25
  • 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. – hrbrmstr Nov 20 '18 at 18:51
  • I'm on rstudio Version 1.3.1093, kableExtra version 1.3.4 and am having the same issues as @GegznaV was back in 2018. !GegznaV, did you find a solution? – cskn Apr 29 '21 at 23:41
  • 1
    @cskn I use RStudio 1.4 and the most current versions of the packages, but the issue persists. – GegznaV Apr 30 '21 at 11:20

1 Answers1

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

the-mad-statter
  • 5,650
  • 1
  • 10
  • 20
  • 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()` – Simbamangu Apr 07 '22 at 04:16