6
votes

I want to have my graph output display on the next slide while the code chunk stays on the first. I am using the default ioslides in Rstudio. I would think that it would be some attribute of the code chunk but I can't figure out what it is.

---
output: ioslides_presentation
---

## Slide with Plot

```{r, echo=TRUE}
plot(cars)
```

Any one have any idea on how to do this in Rstudio?

I want to use this for educational purposes. First showing the code and than revealing the graph. Now I am stuck with doing double code chunks with echo=FALSE and TRUE and eval=FALSE and TRUE.

4

4 Answers

3
votes

To move a plot to the next slide, you need to add a horizontal rule ---- before it (see documentation). You can modify the default plot hook to do it:

```{r setup, include=FALSE}
library(knitr)
local({
  hook_plot = knit_hooks$get('plot')
  knit_hooks$set(plot = function(x, options) {
    paste0('\n\n----\n\n', hook_plot(x, options))
  })
})
```
3
votes

It's easier to run the code twice, once not evaluating the code, the second time not showing the code.

---
title: "Plot Separation"
output: ioslides_presentation
---

## Plot 1

```{r, eval = FALSE}
plot(1:10)
```

## Plot 2

```{r, echo = FALSE}
plot(1:10)
```

It also avoids the error when smaller is true.

1
votes

If you want all your plots to appear on the next slide Yihui's answer is the way to go. But if you want some plots to appear on the same slide you may be better off doing it manually, similar to what you are already doing (and what Dario suggested). Except that I would strongly recommend the use of chunk references. That way you avoid the need to duplicate the code.

---
title: "Plot Separation"
output: ioslides_presentation
---

## Plot code

```{r cars_plot, echo = TRUE, eval = FALSE}
plot(cars)
```

## Plot display

```{r cars_plot, echo = FALSE, eval = TRUE}
```

```

-1
votes

I agree this would be desirable, but I'm finding ioslides to be buggy. Ioslides project started eith google, but was let go a while ago.

You may have better luck with beamer/home slides. But does require a LaTeX distribution to be installed.