创建参数化R降价文档? [英] Create parametric R markdown documentation?

查看:63
本文介绍了创建参数化R降价文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历我的R markdown文件中的结果集列表.当我产生输出时,我想包括一些带有标题的文本,并带有结果集的名称.

I want to iterate over a list of result-sets in my R markdown file. When I produce the output I want to include some text like headers with the name of the result set.

我发现的一种骇人听闻的解决方案是直接在这样的文档中对html输出进行硬编码

One hacky solution I have found is to hardcode the html output directly in the documentation like this

## All results

```{r loopResults, echo=FALSE, results='asis'}
results = list(result1 = data.frame(x=rnorm(3), y=rnorm(3)), result2=data.frame(x=rnorm(3), y=rnorm(3)))

for(res in names(results)) {
  cat(paste("<h3>Results for: ", res, "</h3>>"))

  plot(results[[res]]$x, results[[res]]$y)
}

这似乎不是正确的处理方式,尤其是因为我想一次通过pandoc创建PDF文档,并且必须更改硬编码的表达式. (我目前有一些便利功能,例如h3(text,type)).

This doesn't seem to be the right way to do things, especially since I want to create PDF documents via pandoc at time and would have to change the hard-coded expressions. (I have currently convenience functions like h3(text, type)).

有更好的方法吗?

推荐答案

我将使用brewknitr的组合来实现此目的.我将创建一个名为doc.brew的酿造模板,看起来像这样

I would use a combination of brew and knitr to achieve this. I would create a brew template called doc.brew which looks like this

<% for (res in names(results)) { -%>

### Results for: <%= res %>

```{r}
plot(results[["<%= res %>"]]$x, results[["<%= res %>"]]$y)
```

<% } %>

您现在可以运行以下代码以获取所需的输出

You can now run the following code to get your desired output

results = list(
  result1 = data.frame(x=rnorm(3), y=rnorm(3)), 
  result2=data.frame(x=rnorm(3), y=rnorm(3))
)
brew::brew('doc.brew', 'doc.Rmd')
knit2html('doc.Rmd')

这篇关于创建参数化R降价文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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