在最终输出中显示R markdown块 [英] Show an R markdown chunk in the final output

查看:239
本文介绍了在最终输出中显示R markdown块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Knitr,Markdown和Slidify编写演示文稿.幻灯片将部分与Knitr关联,这就是我坚持解决问题的原因.我不能包括例如knitr-markdown块在幻灯片上显示它.即使我这样做,它总是会在第一次运行时被解释:

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}

```
``` 

如何防止块被解释并因此从最终输出中删除,以便可以显示使用Markdown和Knitr时块的结构?

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?

我尝试了您的版本@Ramnath,并制作了以下幻灯片:

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

前两张幻灯片可以正常工作,但最后一张是问题所在.如果在逐字记录块之前有项目符号列表,则将其解释为通常的格式.因此,它与@Scott中的第一个解决方案相同.我不明白这个.

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.

编辑2/3(有效的解决方案)

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

推荐答案

这是另一个利用块挂钩的解决方案.这个想法是,如果您有一个带有选项verbatim = TRUE的块,它将激活该钩子并逐字输出该块.我已经检查过它也可以与Slidify一起使用.

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

列表后带有代码块的技巧是列表环境需要被破坏.一种快速而肮脏的方法就是添加一个空的段落元素.或者,您可以修复该钩子,以便在代码块的开头自动添加一个空段落.

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

这篇关于在最终输出中显示R markdown块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆