purl() within knit() duplicate label error

后端 未结 2 851
伪装坚强ぢ
伪装坚强ぢ 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

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

    You can avoid this error with a bash chunk that calls purl in a separate R session. That way there's no need to allow duplicate labels.

    An example use case is an Rmd file where the code is run (and not echo'd) throughout the report and then all the code chunks are shown with chunks names and code comments in an Appendix. If you don't require that additional functionality then you would only need up until the bash chunk.

    The idea is that report_end signifies where to stop purl such that the appendix code isn't considered "report code". Then read_chunk reads the entire R file into one code chunk which can then be echo'd with syntax highlighting if required.

    ---
    title: "Purl MWE"
    output: html_document
    ---
    
    These code chunks are used in the background of the report however
    their source is not shown until the Appendix.
    
    ```{r test1, echo=FALSE}
    x <- 1
    x
    ```
    
    ```{r test2, echo=FALSE}
    x <- x + 1
    x
    ```
    
    ```{r test3, echo=FALSE}
    x <- x + 1
    x
    ```
    
    # Appendix
    
    ```{r, eval=TRUE}
    report_end <- "^# Appendix"
    temp <- tempfile(fileext = ".R")
    Sys.setenv(PURL_IN = shQuote("this_file.Rmd"), # eg. knitr::current_input()
               PURL_OUT = shQuote(temp),
               PURL_END = shQuote(report_end))
    ```
    
    
    ```{bash, include=FALSE}
    Rscript -e "lines <- readLines($PURL_IN, warn = FALSE)" \
            -e "knitr::purl(text = lines[1:grep($PURL_END, lines)], output = $PURL_OUT, documentation = 1L)"
    ```
    
    ```{r, include=FALSE}
    knitr::read_chunk(temp, labels = "appendix")
    unlink(temp)
    ```
    
    ```{r appendix, eval=FALSE, echo=TRUE}
    ```
    
    0 讨论(0)
提交回复
热议问题