逐步显示代码,揭示,滑动,Rmarkdown

时间:2014-10-13 17:47:14

标签: r-markdown reveal.js slidify

我正在使用R包滑动来创建revealjs幻灯片。我想使用RMarkdown以递增方式显示代码片段(而不是编辑生成的HTML),但似乎无法找到方法。

在下面的示例中,我有两个代码块,我希望第二个代码块仅在前一段之后发生。我可以查看生成的HTML并将class=fragment添加到pre代码中,但效率非常低!

建议

---
title       : RevealJS with Bootstrap
framework   : revealjs
---

```{r}
mean(1:3)
```

<div class="fragment">
This works fine, the div does not appear until you click forward in the deck. But you can't put the div tags around a code fragment.
</div>


```{r}
mean(1:3)
```

1 个答案:

答案 0 :(得分:1)

您现在可能已经自己回答了问题,但有一种解决方案是使用knitr hooks。这是一个简单的例子:

---
title       : RevealJS with Bootstrap
framework   : revealjs
---

```{r cache=F,echo=F}
s0 <- knitr::knit_hooks$get('source')
o0 <- knitr::knit_hooks$get('output')

knitr::knit_hooks$set(
  list(
    source=function(x,options){
      if (is.null(options$class)) s0(x, options)
      else
        paste0(
          paste0("<div class='", options$class, "'><pre><code>")
          ,x
          ,'</code></pre>\n'
        )
    }
    ,output = function(x,options){
      if (is.null(options$class)) o0(x, options)
      else 
        paste0(
          "<pre><code>"
          ,x
          ,'</code></pre></div>\n'
        )
    }
  )
)
```

```{r class="fragment"}
mean(1:3)
```

<div class="fragment">
  This works fine, the div does not appear until you click forward in the deck. But you can't put the div tags around a code fragment.
</div>


```{r class="fragment"}
mean(1:3)
```