使代码在点击R演示

时间:2016-02-12 02:49:37

标签: r shiny rstudio r-markdown rpres

我目前正在使用Rstudio的R演示功能进行演示。

我有一个算法循环并每隔一两秒绘制一个图形。 可以通过RunAlgorithm()

调用该算法
RunAlgorithm<-function(){
    for(i in 1:10){
        x<-rnorm(1000)
        y<-runif(1000)
        plot(x,y)
        Sys.sleep(1)
    }
}

我想在演示文稿中显示此算法。但是当我在文件中使用它时(下图),我只是获得了每个单独情节的整页,一个在下一个之后显示

enter image description here

TestFile.Rpres

    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

我想要的是点击某个按钮,或转到此幻灯片并在浏览器中播放动画

1 个答案:

答案 0 :(得分:1)

作为任何RMarkdown文档的R演示文稿。不是像动态的东西。一个闪亮的应用。结果是一个静态的html文件。所以你需要的是创建一个可以包含在演示文稿中的动画。这在Combining R Markdown and Animation Package部分得到了解答,但我无法让它为我工作。所以我找到了example

所以对你来说它看起来像是

```{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

编辑:

它也适用于opts_knit$set(animation.fun = hook_ffmpeg_html),但不适用于R-Studio预览,但只有在您点击了更多 - &gt;之后另存为网页....请注意,您必须在路径中安装ffmpeg。