Question / Feature Request code chunk option, "verbatim = TRUE"

It is a bit of a challenge to get a code chunk to be printed verbatim. Yihui has written a very useful blog post: https://yihui.name/en/2017/11/knitr-verbatim-code-chunk/ that explains how to do this, but I find myself forgetting how it works, and not knowing how to search for it when I need it.

What do people think about the idea of a code chunk verbatim = TRUE?

This would parse the rmarkdown chunk for demonstration purposes.

It would mean you wouldn't need to write this in your R code:

````
```{r dev = "jpg"}`r ''`
ggplot(airquality,
       aes(x = Temp,
           y = Wind)) +
    geom_point()
```
````

Instead, you would have the code chunk option like so:

```{r dev = "jpg", verbatim = TRUE}
ggplot(airquality,
       aes(x = Temp,
           y = Wind)) +
    geom_point()
```

And then when knitr/rmarkdown does the magic of creating the document, it adds the appropriate magic spice of the four back ticks, plus r ''. It would feel really slick to have this verbatim feature built into the rmarkdown chunk options.

To me, it means that I don't need to spend time trying to hack my way around this issue, and more time writing code and teaching.

That said, I don't really have a clear idea on just how difficult this would be to implement inside knitr/rmarkdown - perhaps this is indeed just too difficult! I have posted an alternative suggestion to the remedy package: https://github.com/ThinkR-open/remedy/issues/61

7 Likes

I am very enthusiastically in support of this. I also can never remember the magical incantation, and always have to copy and paste from Yihui's blog post. This would be awesome.

Edited to add the myriad of articles that have been posted on this that we ourselves link to on the R Markdown website:

2 Likes

Sounds like I'll have to reconsider if I should add built-in support: https://github.com/yihui/knitr/issues/1688 You are certainly not the first person who wanted it, but I still have ~50 tabs open in my browser... :sob:

3 Likes

I would also appreciate this as I created my own hack using zero-width unicode spaces for R4DS. But I'm not sure about the proposed syntax — for teaching purposees, I think you also need to consider how you'd show the block that generates the verbatim block. In other words, how would you make this literal chunk?

```{r dev = "jpg", verbatim = TRUE}
# This is how you make a verbatim chunk
```
3 Likes

Glad to hear that this is something people are interested in as well!

@hadley - great point. I had not really considered the fact that the verbatim = TRUE bit of it would either naturally show up, or be ignored.

My first guess at what I would expect would be another chunk option called, echo_verbatim = TRUE -
but then we kind of end up in a recursive problem. That is, how do you show the option, verbatim = TRUE - is there another echo_echo_verbatim = TRUE?

For me, here are my ideal solutions:

  1. We can use the four backticks like github does
  2. verbatim = TRUE chunk option, and it comes at the cost of recursion/nesting
  3. a remedy package keyboard shortcut/addin - as I suggested here.
  4. Some dark magic code that you run inside a chunk to tell it, print me verbatim, please?

For four (and this is something I just thought of that might not make any sense at all), I am imagining writing code like this:

```{r dev = "jpg"}
# This is how you make a verbatim chunk
verbatim::print_chunk_verbatim()
```

Which gives the following code:

```{r dev = "jpg"}`r ''`
# This is how you make a verbatim chunk
```

Which in turn renders like:

```{r dev = "jpg"}
# This is how you make a verbatim chunk
```

But I guess to me an ideal situation would be the four backticks.

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