Skip to content

Instantly share code, notes, and snippets.

@mattblackwell
Last active April 14, 2024 20:53
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattblackwell/0d26d5c8f61f231570d61ccd62fe511f to your computer and use it in GitHub Desktop.
Save mattblackwell/0d26d5c8f61f231570d61ccd62fe511f to your computer and use it in GitHub Desktop.
How to create quick and dirty beamer animations with rmarkdown and knitr
---
title: 'Beamer Overlay Test'
output:
beamer_presentation:
incremental: true
theme: CambridgeUS
---
```{r, echo = FALSE}
hook_plot <- knitr::knit_hooks$get("plot")
knitr::knit_hooks$set(
plot = function(x, options) {
if (is.null(options$overlay.plot)) {
return(hook_plot(x, options))
} else {
i <- options$fig.cur
hand <- as.numeric(ifelse(i == options$overlay.plot, 1, 0))
bf <- paste0("\\only<", i, "| handout:", hand, ">{")
paste(c(bf, knitr::hook_plot_tex(x, options), "}"), collapse = "\n")
}
}
)
```
## Frame title
```{r fig, fig.width = 10, fig.height = 7, out.width = "\\textwidth", echo = FALSE, overlay.plot = 1}
set.seed(12345)
x1 <- runif(100)
y1 <- rnorm(100)
cuts <- c(0, 0.25, 0.5, 0.75, 1)
for (i in seq_along(cuts)) {
cols <- ifelse(x1 <= cuts[i], "indianred", "grey80")
plot(x1, y1, col = cols, pch = 19)
abline(v = cuts[i], lwd = 3, col = "indianred")
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment