Create appendix with R-code in rmarkdown/knitr

六眼飞鱼酱① 提交于 2019-12-03 12:03:24

问题


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.

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

Something like this

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

Expected output:

Chunk_1  
y <- 22+325  
y

Chunk_2  
x <- 4+5  
x

回答1:


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}

```



回答2:


Another possibility:

### Appendix 
```{r, ref.label=knitr::all_labels(),echo=TRUE,eval=FALSE}
```

as suggested by Yihui's nice example




回答3:


You could use a reference to your initial chunks, but then change the options:

main text

```{r blah, echo = FALSE}
summary(cars)
```

appendix

```{r blah2, ref.label='blah', eval = FALSE}
```

Which will give:



来源:https://stackoverflow.com/questions/33485893/create-appendix-with-r-code-in-rmarkdown-knitr

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