Rendering code blocks in learnr with backticks

I'm using learnr to build a tutorial for R Markdown. I'd like to include a code block with backticks to show the building of an R Markdown script. However, this is causing issues. I've tried escape characters and varying the number of backticks to start the code block but can't seem to get it working. I believe it is possible as the learnr website does basically what I'm looking to achieve (maybe I'm just looking at it from the wrong angle) - the first code block here: Interactive Tutorials for R • learnr


Referred here by learnr's GitHub

Based on Yihue's StackOverflow answer from about 5 years ago, you can do the following: Fence the whole code section with four backticks, and add `r''` to the beginning of each code chunk. Here's what the document looks like. (Incidentally, I had to fence this whole example with five backticks in order to display it properly.)

---
title: "Hello, Tutorial!"
output: learnr::tutorial
runtime: shiny_prerendered
---

```{r setup, include=FALSE}
library(learnr)
```

````
---
title: "Hello, Tutorial!"
output: learnr::tutorial
runtime: shiny_prerendered
---

`r ''````{r setup, include=FALSE}
library(learnr)
```

This code computes the answer to one plus one, change it so it computes two plus two:

`r ''````{r addition, exercise=TRUE}
1 + 1
```
````

Here's what the output looks like:

Hi joels,

Thanks for this! I did come across it but didn't read it properly. It's not quite the same as the learnr website as the code is unformatted but it's definitely a step closer.

I think this is the Rmd that drives that page. It looks like they have a "loadSnippet" JavaScript function (called on line 41). The JS file gets loaded in the header. And then the snippets themselves are Markdown files.

1 Like

Yep, that looks about right. I can see the Javascript too. Guess I'll need to get my web dev hat on. Thanks Angela_Zoss!

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.