3

I am writing on a presentation using Knitr, Markdown and Slidify. The slides will be partly deal with Knitr as topic which is the reason why I stumbeld upon a problem. I cannot include for example a knitr-markdown chunk to show it on the slide. It will always be interpreted on the first run even if I do something like this:

```
```{r eval = F, include = T}

```
``` 

How can I prevent a chunk to be interpreted and thus removed from the final output so that I can show how a chunk is structured when using Markdown and Knitr?

EDIT:

I tried the version of you @Ramnath and made up te following slides:

## Testslide 1

```{r verbatimchunk, verbatim = TRUE}
x = 1 + 1
x
```

```{r regularchunk}
x = 1 + 1
x
```

---

## Testslide 2

```{r verbatimchunk_2, verbatim = TRUE}
x = 1 + 1
x
```

* element 1
* element 2

---

## Testslide 3

* element 1
* element 2


```{r verbatimchunk_3, verbatim = TRUE}
x = 1 + 1
x
```

The first two slides work fine but the last one is the problem. If there is a bullet list before the verbatim chunk, it is interpreted as usual. So it is the same as with the first solution from @Scott. I do not understand this.

EDIT 2/3 (Working solution)

```{r echo = FALSE}
require(knitr)
hook_source_def = knit_hooks$get('source')
knit_hooks$set(source = function(x, options){
  if (!is.null(options$verbatim) && options$verbatim){
    opts = gsub(",\\s*verbatim\\s*=\\s*TRUE\\s*", "", options$params.src)
    bef = sprintf('\n\n    ```{r %s}\n', opts, "\n")
    stringr::str_c(bef, paste(knitr:::indent_block(x, "    "), collapse = '\n'), "\n    ```\n")
  } else {
     hook_source_def(x, options)
  }
})
```

## Testslide

* Element one
* Element two


Some text here breaks list environment:

```{r verbatim = T}
any code
```
  • Doubt this is possible. Maybe play with `cat()` to literally quote the knitr code you need, though I was able to do that only partially, the final "```" would not render. Alternatively, you could insert a specific set of symbols, such as "/myknitrcode_start" and then find&replace it with the proper code after knitting the original document. Or write an R function to do so. – Maxim.K Nov 11 '13 at 14:36

3 Answers3

4

I think you need to add an empty string after ```{r}, and knitr will not execute the chunk, but will display it. See the example here

This on a slide works for me (where the top one executes and the bottom does not)

---

```{r}
list(5, 6, 7)
```


    ```{r}`r ''`
    hist(rnorm(100))
    5 + 6
    ```

---
sckott
  • 5,755
  • 2
  • 26
  • 42
  • But I have to say that it is a bit brittle. Does not work in all positions of a slidify slide. Only when the chunk is in the first position on a slide. Otherwise it will be just interpreted as usual. –  Nov 11 '13 at 16:38
  • What do you mean by in the first position on a slide? – sckott Nov 11 '13 at 16:59
  • 1
    To get more help on this the knitr google group may be your best bet, thogh Yihui is around SO too – sckott Nov 11 '13 at 23:35
  • For example if I have a list in there. This only works before the list and not in between or after the list on the slide. –  Nov 12 '13 at 08:09
  • @cpfaff I edited the answer above. That example works for me where the top executes and the bottom does not. – sckott Nov 12 '13 at 15:01
  • Well thanks for the update. Yes then it is more brittle than I thought. Sometimes works, sometimes not. If you have a list before the one that you like to show literally it does not work for me. And that is actually the exact thing I need. –  Nov 13 '13 at 07:14
  • 1
    "Sometimes works, sometimes not" is just due to the Markdown syntax: if you indent a paragraph under a list, the paragraph will be treated as part of the list; as Ramnath suggested, you have to terminate the list using `

    ` or something else.
    – Yihui Xie Nov 13 '13 at 19:55
3

Here is another solution that makes use of chunk hooks. The idea is that if you have a chunk with option verbatim = TRUE, it activates the hook and outputs the chunk verbatim. I have checked that it works with Slidify too.

```{r echo = FALSE}
require(knitr)
hook_source_def = knit_hooks$get('source')
knit_hooks$set(source = function(x, options){
  if (!is.null(options$verbatim) && options$verbatim){
    opts = gsub(",\\s*verbatim\\s*=\\s*TRUE\\s*", "", options$params.src)
    bef = sprintf('\n\n    ```{r %s}\n', opts, "\n")
    stringr::str_c(bef, paste(knitr:::indent_block(x, "    "), collapse = '\n'), "\n    ```\n")
  } else {
     hook_source_def(x, options)
  }
})
```

```{r verbatimchunk, verbatim = TRUE}
x = 1 + 1
x
```

```{r regularchunk}
x = 1 + 1
x
```

EDIT: The trick with code chunks after a list is that the list environment needs to be broken. A quick and dirty way is just to add an empty paragraph element. Alternately, you can fix the hook so that en empty paragraph is automatically added at the beginning of the code chunk.

* element 1
* element 2

<p></p>
```{r verbatimchunk_3, verbatim = TRUE}
x = 1 + 1
x
```
Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • I updated my post above. Your solution has the same problems as the one Scott sugested. –  Nov 13 '13 at 08:11
  • If you look at the markdown, you will see that the chunk is output with the chunk wrapper. I presume that markdown still interprets it as a part of the list as it has not been closed. If you add any text before the code chunk, it will break the list and display the output you seek. – Ramnath Nov 13 '13 at 12:00
  • The default hook can be obtained via `knit_hooks$get('source')`, and I prefer not copying its source code from knitr, since it may tie my hands when I want to update it in the future. It will be safer to `hook_source <- knit_hooks$get('source')`, then use `hook_source` in `knit_hooks$set(source = ...)`. – Yihui Xie Nov 13 '13 at 20:00
  • I remember why I hardcoded it. This was around the time when an update in `knitr` broke some things. But you fixed it and so now the approach you recommend is the better one. I will update my answer. – Ramnath Nov 13 '13 at 22:50
0

Very late to the party, but this also seems to work:

```{r echo=FALSE, class.output="r", comment=""}
cat("```{r}\nx <- 1 + 1\nx\n```")
```

Or, equivalent but perhaps nicer to read and write:

```{r echo=FALSE, class.output="r", comment=""}
cat(paste(sep = "\n",
  "```{r}",
  "x <- 1 + 1",
  "x",
  "```"
))
```
Jozef
  • 2,617
  • 14
  • 19