1

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

在下面的示例中,我有两个代码块,我希望第二个代码块仅出现在上一段之后。我可以进入生成的 HTML 并添加class=fragmentpre标签中,但这非常低效!

建议

---
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)
```
4

1 回答 1

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)
```
于 2014-10-17T15:35:13.727 回答