What is the command in RMarkdown to “source” and display the code from an existing .R file?

前端 未结 1 927
青春惊慌失措
青春惊慌失措 2021-01-12 21:41

Example: My R script is named \"code.R\". It produces a simple plot of y versus x. And looks like this in Rmarkdown.

    ````{r eval=FALSE}
    ## code in \"         


        
相关标签:
1条回答
  • 2021-01-12 22:02

    Solution found on: http://yihui.name/knitr/demo/externalization/

    Start your input.R script with the comment "## ---- input.R" (without the quotes)

    Make an .Rmd script with the following code and Knit it. It will show the content of the input.R script in the resulting PDF.

          ---
          output: pdf_document
          ---
    
          ```{r cache=FALSE, echo=FALSE}
          knitr::read_chunk('input.R')
          ```
    
          ```{r input.R, eval=FALSE}
    
          ```
    
    0 讨论(0)
提交回复
热议问题