R Generate slides in slidify presentation in a loop
Asked Answered
T

1

6

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)
}
Taima answered 8/10, 2013 at 15:45 Comment(4)
throw cat("---\n") in your loop to create new slides?Henryhenryetta
I would throw cat("\n\n---\n"), since the separator expects an empty newline before the ---` so that it is not confused with headers.Beadsman
On that note, you can also use a combination or brew + slidify or whisker + slidify to achieve the same. I will cook up a minimal example later today.Beadsman
thanks, I'm a newbie in markdown languages, so the example is greatly appreciatedTaima
T
10

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)
} 
```
Taima answered 10/10, 2013 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.