Create parametric R markdown documentation?

前端 未结 4 1162
盖世英雄少女心
盖世英雄少女心 2020-12-03 06:09

I want to iterate over a list of result-sets in my R markdown file. When I produce the output I want to include some text like headers with the name of the result set.

4条回答
  •  执笔经年
    2020-12-03 06:36

    I would use a combination of brew and knitr to achieve this. I would create a brew template called doc.brew which looks like this

    <% for (res in names(results)) { -%>
    
    ### Results for: <%= res %>
    
    ```{r}
    plot(results[["<%= res %>"]]$x, results[["<%= res %>"]]$y)
    ```
    
    <% } %>
    

    You can now run the following code to get your desired output

    results = list(
      result1 = data.frame(x=rnorm(3), y=rnorm(3)), 
      result2=data.frame(x=rnorm(3), y=rnorm(3))
    )
    brew::brew('doc.brew', 'doc.Rmd')
    knit2html('doc.Rmd')
    

提交回复
热议问题