Generate Dynamic R Markdown Blocks

前端 未结 2 1247
既然无缘
既然无缘 2020-11-29 20:04

In my dataset, I have 60 groups that I want to analyze in put into an HTML report using R Markdown. Because I want to apply the same analysis to each group, I am hoping tha

2条回答
  •  时光说笑
    2020-11-29 20:18

    Try knit_expand():

    Automate Chunks of Analysis in R Markdown 
    ========================================================
    
    ```{r setup, echo=FALSE}
    library(knitr)
    ```
    
    ```{r run-numeric-md, include=FALSE}
    out = NULL
    for (i in as.character(unique(iris$Species))) {
      out = c(out, knit_expand(text='#### Species = {{i}}'))
    }
    ```
    
    `r paste(knit(text = out), collapse = '\n')`
    

    You can also create a template file like 'child.rmd' and put this in your for loop so you don't have to put a complicated analysis in quotes:

    out = c(out, knit_expand('template.rmd'))
    

    Then have your 'template.rmd' be:

    #### Species = {{i}}
    

提交回复
热议问题