将Rmarkdown与Rmarkdown嵌入,而不进行编织评估 [英] Embed Rmarkdown with Rmarkdown, without knitr evaluation

查看:66
本文介绍了将Rmarkdown与Rmarkdown嵌入,而不进行编织评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想演示如何编写RMarkdown,其中所说的RMarkdown演示嵌入在用于创建课程资料的RMarkdown文档中.在这个受限制的代码块中,我希望knitr执行该块.

I want to demonstrate how to write RMarkdown, where said RMarkdown demonstration is embedded within an RMarkdown document used to create the course material. Within this fenced code block, I don't want knitr to execute the chunk.

我想将这样的东西放入我的顶层" Rmarkdown文档中,并在输出HTML文档中以固定的宽度逐字打印外栅栏之间的所有内容,而不是让knitr评估内部嵌入式R代码块和内联代码.

I want to put something like this into my "top level" Rmarkdown document, and have everything that's between the outer fences be printed verbatim in fixed width in the output HTML document, rather than having knitr evaluate the inner embedded R code chunk and inline code.

```
---
title: "RMarkdown teaching demo"
author: "whoever"
---

# Major heading

Here's some text in your RMarkdown document. Here's a code chunk:

```{r, eval=FALSE}
head(mtcars)
```

Now we're back into regular markdown in our embedded document.

Here's inline code that I don't want executed either; 
e.g. mean of mpg is `r mean(mtcars$mpg)`.

```

我尝试了 knitr示例65中的零宽空格技巧,但这在尝试编译为PDF时会失败(我同时需要HTML和PDF).

I've tried the zero-width space trick in knitr example 65, but this fails when trying to compile to PDF (I need both HTML and PDF).

推荐答案

这里是实现它的一种方法.您可以在块头之前添加`r ''`,以便无法识别代码块,然后使用knitr::inline_expr()生成`r `.

Here is one way to achieve it. You may add `r ''` before the chunk header so that the code chunk is not recognized, and use knitr::inline_expr() to generate `r `.

````
---
title: "RMarkdown teaching demo"
author: "whoever"
---

# Major heading

Here's some text in your RMarkdown document. Here's a code chunk:

`r ''````{r, eval=FALSE}
head(mtcars)
```

Now we're back into regular markdown in our embedded document.

Here's inline code that I don't want executed either; 
e.g. mean of mpg is `r knitr::inline_expr('mean(mtcars$mpg)')`.

````

如果仅将R Markdown示例文档保存在一个单独的文件中,然后通过readLines()将其包含在顶级文档中,则将更加容易,例如

It will be easier if you just save the R Markdown example document in a separate file, and include it in the top-level document via readLines(), e.g.

````
`r paste(readLines('example.Rmd'), collapse = '\n')`
````

要在受防护的代码块中包含三个反引号,则需要三个以上的反引号.这就是为什么我在这里使用四个的原因.

To include three backticks in a fenced code block, you need more than three backticks. That is why I'm using four here.

这篇关于将Rmarkdown与Rmarkdown嵌入,而不进行编织评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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