What is the command in RMarkdown for the "source" and display code from an existing .R file?

Example: My R script is called "code.R". It creates a simple graph of y versus x. And it looks like this in Rmarkdown.

    ````{r eval=FALSE}
    ## code in "code.R"
    x = 1:10
    y = 1:10
    plot(x,y)
    ```

For documentation and reproducibility, I want to create an Rmarkdown file that reads "code.R" when knitting from RStudio. (A bit like \ include {} in LaTex.) Thus, the resulting RMarkdown PDF should display an obscene verbatim copy of the R-code from "code.R".

The ultimate goal is to create a RMarkdown file that reads dozens of R files and groups all R codes into one PDF file for reproducibility and future use. This would prevent me from copying the new R-code every time I change the source files. I'm not interested in actually running R code in RMarkdown.

Part of the solution (but how?) Could be to create a fragment that reads the file and stores the read text lines and another fragment that displays these text lines as a shorthand code?

Is there an existing RMarkdown built-in command or additional parameters in `` `` {r eval = FALSE} that give my intended result? Could you give an example?

A welcome link to the more complex Stackoverflow question, which indirectly addresses my issue, is also welcome.

!

+4
1

: http://yihui.name/knitr/demo/externalization/

.R script "## ---- input.R" ( )

.Rmd script . .R script PDF .

      ---
      output: pdf_document
      ---

      ```{r cache=FALSE, echo=FALSE}
      knitr::read_chunk('input.R')
      ```

      ```{r input.R, eval=FALSE}

      ```
+3

All Articles