'Plot on the next slide

I am preparing some slides in RMarkdown, and I need to plot the code on one slide and the plot on the next one, so I frequently find myself doing something on the lines of

```{r, eval=FALSE}
plot(x ,y)
````

---

```{r, echo=FALSE}
plot(x, y)
```

Is there a more elegant way to doing things that would avoid repetitions?



Solution 1:[1]

Instead of that create an object and call it

```{r}
plt <- plot(x ,y)
````

---

```{r, echo=FALSE}
plt
```

Solution 2:[2]

In case you would like to split all your plots over two frames:

---
output: 
  beamer_presentation:
    keep_tex: true
header-includes:
  - \renewenvironment{Shaded}{\begin{onlyenv}<.(1)>\begin{snugshade}}{\end{snugshade}\end{onlyenv}\pause}
---


```{r,  include=TRUE}
plot(pressure)
```

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 samcarter_is_at_topanswers.xyz