生成动态R降价块

Bti*_*rt3 24 r knitr r-markdown

在我的数据集中,我有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'))
}
Run Code Online (Sandbox Code Playgroud)

```

这是child.Rmd.

#### Species = `r [i]`
Run Code Online (Sandbox Code Playgroud)

Sam*_*son 19

试试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')`
Run Code Online (Sandbox Code Playgroud)

您也可以创建一个模板文件'child.rmd',并将其放在for循环中,这样您就不必在引号中放入复杂的分析:

out = c(out, knit_expand('template.rmd'))
Run Code Online (Sandbox Code Playgroud)

那你'template.rmd'有:

#### Species = {{i}}
Run Code Online (Sandbox Code Playgroud)

  • 您在“for”循环中创建的是一个字符向量,但您希望它像文件的其余非代码部分一样使用字符返回(“\n”)分隔不同的行,因此这是通过折叠“\n”上的向量来发生的。 (2认同)

小智 6

采用@ sam的解决方案,我做了以下通用功能.假设您grfDf在列中使用DiagrammeR图形对象调用了数据框graph.以下是在Rmd中绘制所有图形所需的全部内容: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')
}
```
Run Code Online (Sandbox Code Playgroud)