R, knitr, and source function: How to preserve source file comments for html report

后端 未结 2 1493
攒了一身酷
攒了一身酷 2021-01-11 22:17

R console: When I call source(\"file_of_functions.R\",echo=TRUE), all source file expressions, including comments, print to console.

相关标签:
2条回答
  • 2021-01-11 22:33

    test.Rmd

    ---
    output: html_document
    ---
    
    ```{r}
    options(prompt = '> ')
    ```
    
    ```{r}
    source('./test.r', echo = TRUE)
    ```
    
    ```{r}
    source('./test.r', echo = TRUE, keep.source = TRUE)
    ```
    

    enter image description here

    0 讨论(0)
  • 2021-01-11 22:33

    I don't mean to post this as an answer, but I just want to point out the possibility that you can easily insert test.r into a code chunk using

    ```{r code=readLines('test.r')}
    ```
    

    Personally I think this is much nicer than using source(), e.g. you don't get the prompts > by default (you can if you want), and the R code will be syntax highlighted. Of course, your comments will be preserved.

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