Show an R markdown chunk in the final output

早过忘川 提交于 2019-11-28 05:27:05

问题


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

回答1:


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



回答2:


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

---



回答3:


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",
  "```"
))
```


来源:https://stackoverflow.com/questions/19908158/show-an-r-markdown-chunk-in-the-final-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!