I'm currently making a presentation using Rstudio's R presentation functionality.
I have an algorithm which loops through and plots a graph every second or two.
The algorithm can be called via RunAlgorithm()
RunAlgorithm<-function(){
for(i in 1:10){
x<-rnorm(1000)
y<-runif(1000)
plot(x,y)
Sys.sleep(1)
}
}
I want to show this algorithm running in the presentation. But when I use it in the file (below), I just get a whole page of each individual plot, one displayed right after the next

TestFile
========================================================
author: me
date: today
First Slide
========================================================
```{r echo = FALSE}
RunAlgorithm<-function(){
for(i in 1:10){
x<-rnorm(1000)
y<-runif(1000)
plot(x,y)
Sys.sleep(1)
}
}
RunAlgorithm()
```
Slide With Code
========================================================
Slide 2
What I would like is some way to click a button, or turn onto this slide and have the animation play in browser
A R presentation as any RMarkdown document. is not something dynamic like eg. a shiny app. The result is a static html file. So what you need is to create an animation that you can include in the presentation. This is partly anwered in Combining R Markdown and Animation Package but I could not get it to work for me. So I found this example.
So for you it would look like
```{r setup,echo=FALSE}
library(knitr)
opts_knit$set(animation.fun = hook_scianimator)
```
<link rel="stylesheet" href="http://vis.supstat.com//assets/themes/dinky/css/scianimator.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://vis.supstat.com/assets/themes/dinky/js/jquery.scianimator.min.js"></script>
TestFile
========================================================
author: me
date: today
First Slide
========================================================
```{r echo = FALSE, fig.show='animate', interval=1}
RunAlgorithm<-function(){
for(i in 1:10){
x<-rnorm(1000)
y<-runif(1000)
plot(x,y)
}
}
RunAlgorithm()
```
Slide With Code
========================================================
Slide 2
EDIT:
It also works with opts_knit$set(animation.fun = hook_ffmpeg_html) but not in the R-Studio preview but only after you clicked on More -> Save As Web Page... . Note, that you have to have ffmpeg installed and in your path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With