1
votes

The output of Rmarkdown code chunks is formatted as R code using knitr. While this works fine for light themes (black font on white background), this behaviour is a pain for dark themes.
The example shows the 'Dracula' theme of the highlight.js library using the R package reportMD.

1) Rmarkdown (reportMD)

---
output:
  reportMD::multi_document:
    toc: true
    fig_caption: true
    highlight: dracula 
params:
  version: !r 
---

```{r}
head(mtcars)
```

enter image description here

```{r, results='asis'}
head(mtcars)
```

enter image description here

Desired colouring of R code chunks would equal plain markdown colouring:

```
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
```

enter image description here

However, for the R presentation package xaringan, also highlight.js is used for syntax highlighting. The engine behind xaringan is remark.js. When evaluating R code chunks here, the R source is highlighted and the output is displayed as plain text.

2) xaringan

---
output:
  xaringan::moon_reader:
    lib_dir: libs
    nature:
      highlightStyle: dracula
      highlightLines: true
      countIncrementalSlides: false
---

```{r}
head(mtcars)
```

enter image description here

I do not what is going on in detail behind the scenes (xaringan + remark vs. knitr + rmarkdown).
A wonderful option would be to have a chunk option to turn plain text code output on & off.
The desired output is 2)

1
As in results='asis'? hightlight=FALSE? Or highlight in the YAML front matter? You need to make your example reproducible.alistaire
@alistaire added code to reproduce it. Chunk option results='asis' does not render the output correctly. Btw: I would prefer critical comments/questions rather than direct closing votes...pat-s
"the desired output is 2" I don't understand, you want a dark theme but output 2, the image in the xarigan section has a white background?Paul Rougieux
@PaulRougieux I want either "white background with black font color" like in xaringan OR "black background with white font color" as in plain markdown code chunks using 'Dracula' theme. However, I only get bluish font color on black background when using R code chunks. Thats because the chunk output is formatted and coloured as R comments.pat-s

1 Answers

1
votes

Setting knitr::opts_chunk$set(comment = "") prevents knitr and highlight.js from formatting the chunk output as comments and therefore no comment colouring is applied.

enter image description here