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
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:
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
```
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:
We can use the four backticks like github does
verbatim = TRUE chunk option, and it comes at the cost of recursion/nesting
a remedy package keyboard shortcut/addin - as I suggested here.
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.
As an update to this topic of verbatim content, just wanted to share that current dev version of knitr (will be 1.37) supports a new verbatim engine to simplify this
````{verbatim, lang = "markdown"}
```{r dev = "jpg"}
ggplot(airquality,
aes(x = Temp,
y = Wind)) +
geom_point()
```
````
will produce this
````markdown
```{r dev = "jpg"}
ggplot(airquality,
aes(x = Temp,
y = Wind)) +
geom_point()
```
````