【问题标题】:Incrementally reveal code, revealjs, slidify, Rmarkdown增量显示代码、revealjs、slidify、Rmarkdown
【发布时间】:2014-10-13 17:47:14
【问题描述】:

我正在使用 R 包 slidify 来创建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)
```

【问题讨论】:

    标签: r-markdown reveal.js slidify


    【解决方案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)
    ```
    

    【讨论】:

    • 这非常接近,所以我标记为答案。但是这样做的一个问题是它不适用于 collapse=T​​RUE 这对我的幻灯片很重要。我添加了一个解决此问题的要点,但它不适用于具有绘图的代码块。 gist.github.com/zross/6fe6c9d494646245a864
    • 我无法回答整个问题,但我修改了@timelyportfolio 的答案以使用绘图(我不需要在演示文稿中显示代码)gist.github.com/srvanderplas/68d73ee4c6fdead4ed32
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2020-02-02
    • 2017-09-30
    • 2014-02-08
    • 1970-01-01
    • 2015-11-11
    • 2015-07-06
    相关资源
    最近更新 更多