简体   繁体   中英

Create appendix with R-code in rmarkdown/knitr

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
x <- 4+5  
x

Chunk_2  
y <- 22+325  
y

Another possibility:

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

as suggested by Yihui's nice example

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}

```

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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