Highlight lines within code chunks


#1

I would like to highligh certain lines of code chunk of a Rmarkdown document. I have discovered the "xaringa" which uses a particular template (different from Rmarkdown) to generate HTML only slides. Following, there is a minimal example:

example

---
title: "Presentation Ninja"
output:
  xaringan::moon_reader:
    nature:
      highlightLines: true
---

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
```

# Some Tips

An example of using the trailing comment `#<<` to highlight lines:

```{r tidy=FALSE, eval=FALSE}
library(ggplot2)
ggplot(mtcars) + 
  aes(mpg, disp) + 
  geom_point() +   #<<
  geom_smooth()    #<<
```

end example

The problem is that I would like to compile it both as PDF slides and HTML slides, preferible with standard Rmarkdown templates.

Isaac.


#2

I'm not sure it is a supported workflow since xaringa is based on JavaScript, so all of the highlighting is done there. Not sure, if it is transferable to PDF.


#3

The xaringan wiki has a page on exporting to PDF (using the webshot package):