purl() within knit() duplicate label error

后端 未结 2 877
伪装坚强ぢ
伪装坚强ぢ 2021-02-08 04:08

I am knitting a .Rmd file and want to have two outputs: the html and a purl\'ed R script each time I run knit. This can be done with the following Rmd file:

---         


        
2条回答
  •  一向
    一向 (楼主)
    2021-02-08 05:04

    You can allow duplicate labels by including options(knitr.duplicate.label = 'allow') within the file as follows:

    title: "Purl MWE"
    output: html_document
    ---
    
    ```{r GlobalOptions}
    options(knitr.duplicate.label = 'allow')
    ```
    
    
    ```{r}
    ## This chunk automatically generates a text .R version of this script when     running within knitr.
    input  = knitr::current_input()  # filename of input document
    output = paste(tools::file_path_sans_ext(input), 'R', sep = '.')
    knitr::purl(input,output,documentation=1,quiet=T)
    ```
    
    
    ```{r test}
    x=1
    x
    ```
    

    This code isn't documented on the knitr website, but you can keep track with the latest changes direct from Github: https://github.com/yihui/knitr/blob/master/NEWS.md

提交回复
热议问题