Proper R Markdown Code Organization

前端 未结 2 1625
轻奢々
轻奢々 2021-01-13 13:55

I have been reading about R Markdown (here, here, and here) and using it to create solid reports. I would like to try to use what little code I am running to do some ad hoc

2条回答
  •  隐瞒了意图╮
    2021-01-13 14:22

    There is a solution for this sort of problem, explained here.

    Basically, if you have an .R file containing your code, there is no need to repeat the code in the .Rmd file, but you can include the code from .R file. For this to work, the chunks of code should be named in the .R file, and then can be included by name in the .Rmd file.

    test.R:

    ## ---- chunk-1 ----
    table(cars.by.name$make)
    

    test.Rmd

    Just once on top of the .Rmd file:

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

    For every chunk you're including (replace chunk-1 with the label of that specific chunk in your .R file):

    ```{r chunk-1}
    ```
    

    Note that it should be left empty (as is) and in run-time your code from .R will be brought over here and run.

提交回复
热议问题