Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show an R markdown chunk in the final output

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
```

2 Answers

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
    ```

---
like image 95
sckott Avatar answered Feb 01 '26 02:02

sckott


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
```
like image 23
Ramnath Avatar answered Feb 01 '26 02:02

Ramnath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!