Create appendix with R-code in rmarkdown/knitr

前端 未结 3 1063
暖寄归人
暖寄归人 2021-02-04 05:04

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 out         


        
相关标签:
3条回答
  • 2021-02-04 05:53

    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}
    
    ```
    
    0 讨论(0)
  • 2021-02-04 05:55

    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:

    0 讨论(0)
  • 2021-02-04 06:05

    Another possibility:

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

    as suggested by Yihui's nice example

    0 讨论(0)
提交回复
热议问题