0

Consider this simple example:

---
title: "Untitled"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
## Slide with R Output
```{r t,  warning=FALSE, message=FALSE}

library(knitr)
library(kableExtra)
library(dplyr)

for(threshold in c(20, 25)) {
  cars %>% 
    filter(dist < threshold) %>%
    kable('html') %>% 
    kable_styling(bootstrap_options = "striped") 
}
```

Here I simply want to print each output of the for loop into a different slide. In this example, there are two calls to kablethat should go on two different slides.

The code above does not work. Am I even using the right packages for that? Any ideas?

Thanks!

ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235

3 Answers3

2

You can use the asis option:

---
title: "Untitled"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
library(kableExtra)
library(dplyr)
# needed so r will include javascript/css dependencies needed for striped tables:
kable(cars, "html") %>% kable_styling(bootstrap_options = "striped")
```

```{r, results = "asis"}
for (threshold in c(20, 25)) {
  cat("\n\n##\n\n")
  x <- cars %>%
    filter(dist < threshold) %>%
    kable('html') %>%
    kable_styling(bootstrap_options = "striped")
  cat(x)
}
```
Chris
  • 1,575
  • 13
  • 20
  • 1
    yep, apparently rmarkdown won't include dependencies in the html file unless it thinks it needs to...since the output of the chunk was raw text, rmarkdown didn't think to look for required dependencies to include in the file, hence whatever was needed to render the stripped table was not included. i've edited the answer so that a dummy table is included, which will result in rmarkdown including the required files. – Chris Jul 05 '18 at 21:39
  • Apparently the `cars` data frame only has two columns `(View(cars))`...maybe you were thinking of using `mtcars` in your example instead of `cars`? i see i mix and matched the data sets myself in the answer. i've edited my answer so it just references the `cars` data set. – Chris Jul 05 '18 at 21:59
2

To get rid of that bogus table, you can try to put options(kableExtra.html.bsTable = T) in your setup section.

Hao
  • 7,476
  • 1
  • 38
  • 59
1

Here's the start of a solution. You can print strings with markdown, either by making the strings yourself or using pander's pandoc.* functions. If you set results="asis" for that chunk, it will get compiled the same as any other markdown. I used cat to make the ## headings, but commented out two pander functions that you could try also to make headers or horizontal rules to split slides.

There's more detail on the pander functions here, plus other SO questions such as this one.

---
title: "Untitled"
output: ioslides_presentation
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(knitr)
library(kableExtra)
library(dplyr)

```


```{r, results='asis'}
for(threshold in c(20, 25)) {
  # pander::pandoc.header(sprintf("Threshold = %s", threshold))
  # pander::pandoc.horizontal.rule()
  cat(paste("\n##", "Threshold =", threshold), "\n")

  tbl <- cars %>% 
    filter(dist < threshold) %>%
    kable(format = "html") %>%
    kable_styling(bootstrap_options = "striped")
  print(tbl)
}
```

One issue is that when I knit this, I'm not getting the striped table that you'd expect. If I add a slide before this chunk and put a table in it with these kableExtra settings, I do get stripes, but the first table is also pretty ugly...I'm not sure if that's a bug or conflicting CSS somewhere or what.

camille
  • 16,432
  • 18
  • 38
  • 60
  • thanks camille but the issue is that i want to keep kableextras' formatting magic – ℕʘʘḆḽḘ Jul 05 '18 at 21:23
  • in particular the striping :) – ℕʘʘḆḽḘ Jul 05 '18 at 21:27
  • I'm not sure what's up with that exactly—I'm getting different results each time I mess around with it, none of them good. I think now the issue isn't generating tables from a loop, but getting `kableExtra` and `ioslides`, and the CSS of each, to coexist how you want. I'm finding a couple SO posts and github issues related to that – camille Jul 05 '18 at 21:41