【问题标题】:code chunk how to loop with knit_expand plots results asis with headings?代码块如何使用 knit_expand 循环绘制结果与标题?
【发布时间】:2020-06-16 22:56:07
【问题描述】:

我正在阅读类似的问题(hereherehere 以及 here),但未能解决这个问题,可能是由于列表中混合了 grobs 和 rastergrobs循环内。本质上,我有一个嵌套的 for 循环来打印列表中的对象,并希望使用 markdown 的对象的标题。我将使用带有子模板的 knit_expand 方法。如果我设置 results='asis' 标题工作但情节没有,如果我删除 results='asis' 情节工作但不是标题。有什么解决办法吗?这是一个可重现的示例,尽管子列表是在这里设计的并且工作不完全正确,但与我正在做的事情很接近,但想法是相同的(需要成为 asis 和 plot 对象的标题的混合):

主模板(Master.Rmd)

    ---
    title: "Example"
    output:
      html_document:
        toc: true
        toc_float: true
        toc_depth: 2
    ---
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = FALSE)
    library(knitr)
    library(datasets)
    library(ggplot2)
    library(data.table)
    library(png)
    library(grid)
    library(gridExtra)
    library(RCurl)
    data(iris)
    plotsList = vector(mode = "list", length = length(levels(iris$Species)))
    names(plotsList) = levels(iris$Species)
    irisDT = as.data.table(iris)
    dataInList = split(irisDT, by="Species")
    for(i in seq(plotsList)){
      plotsList[[i]]$example_plot[1] <- list(ggplot(data=dataInList[[i]], aes(x = Sepal.Length, y = Sepal.Width)) +
                                              geom_point() + xlab("Sepal Length") + ylab("Sepal Width") +
                                              ggtitle(paste0("Sepal Length-Width for ", names(plotsList[i]))))
      plotsList[[i]]$example_plot[2] <- list(ggplot(data=dataInList[[i]], aes(x = Petal.Length, y = Petal.Width)) +
                                              geom_point() + xlab("Petal Length") + ylab("Petal Width") +
                                              ggtitle(paste0("Petal Length-Width for ", names(plotsList[i]))))
      plotsList[[i]]$png_example <- rasterGrob(readPNG(getURLContent(("https://i.imgur.com/mfuTUPD.png"))))
    }
    ```

    # Big Heading 

    some text

    ## Other headings

    other text

    # Loop output Description {.tabset .tabset-fade}

    text about loop output

    ```{r run_loop, echo=FALSE}
    # for the headings to work, results need to be asis, but then the plots don't work :(
    out = NULL
    out2 = NULL
    for(i in seq(plotsList)){
      cat("\n")
      cat("## ", {{names(plotsList[i])}}, "\n")
      out = c(out, knit_expand('one_level.Rmd'))
      for(j in seq(plotsList[[i]][['example_plot']])){
        out2 = c(out2, knit_expand('two_level.Rmd'))
      }
    }
    ```

    `r paste(knit(text = out), collapse = '\n')`
    `r paste(knit(text = out2), collapse = '\n')`
    ```

和子模板(Child.Rmd):

    ```{r echo=FALSE}
      grid.arrange(plotsList[[i]][['example_plot']][[j]])
    ```

    ```{r echo=FALSE, results='asis'}
      grid.arrange(plotsList[[i]][['png_example']])
    ```

【问题讨论】:

    标签: r loops markdown knitr


    【解决方案1】:

    我确定还有其他方法可以做到这一点,但我最终想出了如何将标题降价直接添加到 master.Rmd 的 knit expand 输出中,如下所示:

      `out = c(out, knit_expand(text = paste0("\n## ", {{names(combined.plots[i])}},  "\n  ")))`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2018-09-06
      • 2016-06-30
      • 1970-01-01
      • 2019-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多