簡體   English   中英

生成動態R降價塊

[英]Generate Dynamic R Markdown Blocks

在我的數據集中,我有60個組要分析,並使用R Markdown將其放入HTML報告中。 因為我想對每個組應用相同的分析,我希望有一種方法可以動態生成代碼塊/分析。

簡單地說,我想避免復制塊60次。

我碰到這個這個問題,它使用兒童knitr 我試圖用虹膜數據集復制它。 在下面的例子中,我想做的就是生成三個H4標題,每個標題一個。

值得注意的是,我沒有嫁給這種方法,它似乎與我想要做的事情有關。

這是我使用的文件:

parent.RMD文件。 這將是我的“主人”報告。

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_child('child.Rmd'))
}

```

這是child.Rmd

#### Species = `r [i]`

試試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')`

您還可以創建一個像'child.rmd'這樣的模板文件,並將其放在for循環中,這樣您就不必在引號中放入復雜的分析:

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

然后讓你的'template.rmd'成為:

#### Species = {{i}}

采用@ sam的解決方案,我做了以下通用功能。 假設您在列graph有一個名為grfDf的數據框, grfDf包含DiagrammeR圖形對象。 以下是您在Rmd: r require(DiagrammeR); renderHtmlWidgetList(grfDf$graph, render_graph)繪制所有圖形所需的全部內容r require(DiagrammeR); renderHtmlWidgetList(grfDf$graph, render_graph) r require(DiagrammeR); renderHtmlWidgetList(grfDf$graph, render_graph) 請參閱警告代碼。

```
require(knitr)

#' Render a list of htmlWidgets using various tricks
#'
#' @param widgetList A list of htmlWidget objects to be rendered
#' @param renderFunction The function to render individual widgets. It can be either a name
#'   of the rendering function, e.g., "render_graph" in DiagrammeR, or the actual function to
#'   be passed to this call.
#' @return The knitted string. This is to be included in the output by using `r renderHtmlWidgetList(...)`;
#' @details This is a collection of various tricks. See the URL citations in the code.
#'   Note that this code does alliterate global variables starting with "renderHtmlWidgetList_".
#'   You may want to delete them using rm(list = ls(pattern="renderHtmlWidgetList_*")).
#' @examples Inlcude the following in the Rmd directly
#'   `r require(DiagrammeR); renderHtmlWidgetList(grfDf$graph, render_graph)`
#'
#' @export

renderHtmlWidgetList <- function(widgetList, renderFunction){
  # error checking
  stopifnot(is.list(widgetList))
  # handles if the renderFunction is actually a function
  # http://stackoverflow.com/questions/10520772/in-r-how-to-get-an-objects-name-after-it-is-sent-to-a-function
  if(is.function(renderFunction)) {
    # convert back to string, because we need to knit it later
    renderFunction <- deparse(substitute(renderFunction))
  }
  stopifnot(is.character(renderFunction) & length(renderFunction)==1)
  stopifnot(exists(renderFunction, mode = "function"))
  # inject global vars; make sure we have a unique global var name
  gVarName<- paste0("renderHtmlWidgetList_", sample(1:10000, 1))
  while (exists(gVarName)) {
    gVarName<- paste0("renderHtmlWidgetList_", sample(1:10000, 1))
  }
  # assigning widgetList to a global temp var
  # http://stackoverflow.com/questions/5510966/create-a-variable-name-with-paste-in-r
  assign(gVarName, widgetList, envir = .GlobalEnv)
  # solution from https://gist.github.com/ReportMort/9ccb544a337fd1778179
  out <- NULL
  knitPrefix <- "\n```{r results='asis', cache=FALSE, echo=FALSE}\n\n"
  knitSuffix <- "\n\n```"
  for (i in 1:length(widgetList)) {
    knit_expanded <- paste0(knitPrefix, renderFunction, "(", gVarName, "[[", i, "]])")
    out = c(out, knit_expanded)
  }
  #invisible(out)
  paste(knitr::knit(text = out), collapse = '\n')
}
```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM