R knitr:可以以编程方式修改组块标签吗? [英] R knitr: Possible to programmatically modify chunk labels?

查看:74
本文介绍了R knitr:可以以编程方式修改组块标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用knitr生成一个报告,该报告对数据集的不同子集执行相同的分析集.该项目包含两个Rmd文件:第一个文件是用于设置工作区的主文档,而第二个文件仅包含执行分析并生成关联图的块.

I'm trying to use knitr to generate a report that performs the same set of analyses on different subsets of a data set. The project contains two Rmd files: the first file is a master document that sets up the workspace and the document, the second file only contains chunks that perform the analyses and generates associated figures.

我想做的是编织主文件,然后该主文件将为每个数据子集调用第二个文件,并将结果包含在单个文档中.下面是一个简单的示例.

What I would like to do is knit the master file, which would then call the second file for each data subset and include the results in a single document. Below is a simple example.

主文档:

# My report

```{r}
library(iterators)
data(mtcars)
```

```{r create-iterator}
cyl.i <- iter(unique(mtcars$cyl))
```

## Generate report for each level of cylinder variable
```{r cyl4-report, child='analysis-template.Rmd'}
```

```{r cyl6-report, child='analysis-template.Rmd'}
```

```{r cyl8-report, child='analysis-template.Rmd'}
```

analysis-template.Rmd:

analysis-template.Rmd:

```{r, results='asis'}
cur.cyl <- nextElem(cyl.i)
cat("###", cur.cyl)
```

```{r mpg-histogram}
hist(mtcars$mpg[mtcars$cyl == cur.cyl], main = paste(cur.cyl, "cylinders"))
```

```{r weight-histogam}
hist(mtcars$wt[mtcars$cyl == cur.cyl], main = paste(cur.cyl, "cylinders"))
```

问题是knitr不允许使用非唯一的块标签,因此当第二次调用analysis-template.Rmd时,编织失败.通过保留未命名的块可以避免此问题,因为这样会自动生成唯一的标签.但是,这并不理想,因为我想使用块标签为导出的图创建信息丰富的文件名.

The problem is knitr does not allow for non-unique chunk labels, so knitting fails when analysis-template.Rmd is called the second time. This problem could be avoided by leaving the chunks unnamed since unique labels would then be automatically generated. This isn't ideal, however, because I'd like to use the chunk labels to create informative filenames for the exported plots.

一种可能的解决方案是使用一个简单的函数,该函数将当前圆柱体附加到块标签:

A potential solution would be using a simple function that appends the current cylinder to the chunk label:

```r{paste('cur-label', cyl, sep = "-")}
```

但是,似乎knitr不会在块标签位置评估表达式.

But it doesn't appear that knitr will evaluate an expression in the chunk label position.

我还尝试使用自定义的 chunk hook 来修改当前块的标签:

I also tried using a custom chunk hook that modified the current chunk's label:

knit_hooks$set(cyl.suffix = function(before, options, envir) {
    if (before) options$label <- "new-label"
})

但是更改块标签并不会影响生成的图的文件名,因此我不认为knitr在利用新标签.

But changing the chunk label didn't affect the filenames for generated plots, so I didn't think knitr was utilizing the new label.

关于如何更改组块标签以便可以多次调用同一子文档的任何想法?还是实现这一目标的替代策略?

Any ideas on how to change chunk labels so the same child document can be called multiple times? Or perhaps an alternative strategy to accomplish this?

推荐答案

对于遇到此帖子的其他人,我想指出@Yihui提供了

For anyone else who comes across this post, I wanted to point out that @Yihui has provided a formal solution to this question in knitr 1.0 with the introduction of the knit_expand() function. It works great and has really simplified my workflow.

例如,以下内容将为mtcars$cyl的每个级别处理以下模板脚本,每次将{{ncyl}}(模板中)的所有实例替换为其当前值:

For example, the following will process the template script below for every level of mtcars$cyl, each time replacing all instances of {{ncyl}} (in the template) with its current value:

# My report

```{r}
data(mtcars)
cyl.levels <- unique(mtcars$cyl)
```

## Generate report for each level of cylinder variable
```{r, include=FALSE}
src <- lapply(cyl.levels, function(ncyl) knit_expand(file = "template.Rmd"))
```

`r knit(text = unlist(src))`

模板:

```{r, results='asis'}
cat("### {{ncyl}} cylinders")
```

```{r mpg-histogram-{{ncyl}}cyl}
hist(mtcars$mpg[mtcars$cyl == {{ncyl}}], 
  main = paste({{ncyl}}, "cylinders"))
```

```{r weight-histogam-{{ncyl}}cyl}
hist(mtcars$wt[mtcars$cyl == {{ncyl}}], 
  main = paste({{ncyl}}, "cylinders"))
```

这篇关于R knitr:可以以编程方式修改组块标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆