Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Generate slides in slidify presentation in a loop

I want to create N slides to report descriptive statistics for N subsets of (big) data using slidify package . In an earlier discussion Create parametric R markdown documentation? the combination of brew and knitr were advised to achieve this.

I'm wondering whether slidify has its own workaround for such a task? I guess iterating through data to populate slides is even more logical than for plain text...

A minimal example (from the question above, I need slides instead of paragraphs)

```{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)
}
like image 709
RInatM Avatar asked Oct 08 '13 15:10

RInatM


Video Answer


1 Answers

For those interested, this is what I have by far (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)
} 
```
like image 171
RInatM Avatar answered Oct 10 '22 01:10

RInatM