在rmarkdown/knitr中使用R代码创建附录 [英] Create appendix with R-code in rmarkdown/knitr

查看:222
本文介绍了在rmarkdown/knitr中使用R代码创建附录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在附录中获取所有代码.假设我在一个文档中有两个大块,然后是一些文本.

Is it possible to get all of the code in a appendix. Say I have two chunks in a document and then some text.

```{r, echo=TRUE}
x <- 4+5
x
```  
Above is X output.

```{r, echo=TRUE}
y <- 22+325
y
```   

Above is Y output.


然后我希望将所有代码都放在附录中,但是就像我将eval=FALSE放在块中一样.


And then I want all of the code in a appendix but shown as if I put eval=FALSE in the chunk.

类似这样的东西

```{r, SHOW_ALL_CODE=TRUE}
```

预期输出:

Chunk_1
x <- 4+5  
x

Chunk_2  
y <- 22+325  
y

推荐答案

knitr::purl()可以将Markdown文件中的所有R代码提取到R脚本中.您可以将其添加为附录.

knitr::purl() can extract all R code from a markdown file into an R script. You can add that as an appendix.

## appendix

```{r code=readLines(knitr::purl('~/path/to/file.Rmd', documentation = 0)), eval = FALSE}

```

这篇关于在rmarkdown/knitr中使用R代码创建附录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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