R以循环方式在滑动演示中生成幻灯片

时间:2013-10-08 15:45:16

标签: r markdown r-markdown slidify

我想创建N张幻灯片,以使用slidify包报告N个(大)数据子集的描述性统计信息。在之前的讨论Create parametric R markdown documentation?中,建议brewknitr的组合实现此目标。

我想知道slidify是否有针对此类任务的解决方法?我想迭代数据来填充幻灯片比纯文本更合乎逻辑......

一个最小的例子(从上面的问题来看,我需要幻灯片而不是段落)

```{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)
}

1 个答案:

答案 0 :(得分:8)

对于那些感兴趣的人,这就是我到目前为止(for-loop + two column layout(as here)))

---
title: Loop test
widgets: [bootstrap, quiz]
---
## Simplest Example##

```{r echo = F, fig.width = 12, fig.height = 7,  results='asis'}
library(data.table)
data <- as.data.table(list(days = 1:100, revenue = runif(10, 1, 100), profit = runif(10, 1, 10), department = factor(sample(letters[1:3]))))
for(j in levels(data$department)) {
dataj <-  data[data$department == j,]    
cat("\n\n--- &twocol\n")
cat(paste("\n\n## Department: ", j, "##\n")  )  
cat("\n*** left\n")
cat(paste("\nMean profit = ", round(mean(dataj$profit)), "\n")) 
cat(paste("\nMean revenue = ", round(min(dataj$revenue))), "\n")

cat("\n*** right\n\n")
z<-ggplot(data = dataj ) +
geom_density(aes(x = profit), alpha = 0.3) +
geom_density(aes(x = revenue), alpha = 0.3) 
print(z)
} 
```