pat-s
pat-s

Reputation: 6312

Option for plain text formatting of Rmarkdown chunk output?

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)

Upvotes: 1

Views: 1887

Answers (1)

pat-s
pat-s

Reputation: 6312

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

Upvotes: 2

Related Questions