我正在使用R包slitify创建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)
```
您现在可能已经自己回答了,但一种解决方案是使用编织器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)
```