简体   繁体   English

在最终输出中显示 R markdown 块

[英]Show an R markdown chunk in the final output

I am writing on a presentation using Knitr, Markdown and Slidify.我正在使用 Knitr、Markdown 和 Slidify 编写演示文稿。 The slides will be partly deal with Knitr as topic which is the reason why I stumbeld upon a problem.幻灯片将部分处理 Knitr 作为主题,这就是我偶然发现问题的原因。 I cannot include for example a knitr-markdown chunk to show it on the slide.我不能包括例如一个 knitr-markdown 块来在幻灯片上显示它。 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?如何防止块被解释并因此从最终输出中删除,以便我可以在使用 Markdown 和 Knitr 时显示块的结构?

EDIT:编辑:

I tried the version of you @Ramnath and made up te following slides:我尝试了你@Ramnath 的版本并制作了以下幻灯片:

## 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.所以它与@Scott 的第一个解决方案相同。 I do not understand this.我不明白。

EDIT 2/3 (Working solution)编辑 2/3(工作解决方案)

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

I think you need to add an empty string after ```{r} , and knitr will not execute the chunk, but will display it.我认为您需要在```{r}之后添加一个空字符串,并且 knitr 不会执行该块,但会显示它。 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
    ```

---

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.这个想法是,如果你有一个带有选项verbatim = TRUE的块,它会激活钩子并逐字输出块。 I have checked that it works with Slidify too.我已经检查过它是否也适用于 Slidify。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM