使代码和输出在R演示中逐渐显示

时间:2018-01-22 16:03:35

标签: r r-markdown

我知道可以制作bulleted lists appear incrementally

是否可以对代码示例和该代码的输出执行相同操作,无论输出是文本还是绘图?

修改

当我运行@ MartinSchmelzer的代码时,我收到此错误:

pandoc: Could not fetch https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js
HttpExceptionRequest Request {
  host                 = "ajax.googleapis.com"
  port                 = 443
  secure               = True
  requestHeaders       = []
  path                 = "/ajax/libs/jquery/3.2.1/jquery.min.js"
  queryString          = ""
  method               = "GET"
  proxy                = Nothing
  rawBody              = False
  redirectCount        = 10
  responseTimeout      = ResponseTimeoutDefault
  requestVersion       = HTTP/1.1
}
 (InternalException (HandshakeFailed Error_EOF))
Error: pandoc document conversion failed with error 67
Execution halted

编辑2

这里提出的解决方案并不奏效。 https://github.com/jimhester/knitrBootstrap/issues/172添加

output:
  html_document:
    self_contained: no

到yaml标题允许文档编织,但图像不再编织,看起来是笔记本格式。

1 个答案:

答案 0 :(得分:2)

如果您使用Reveal演示文稿,则可以执行以下操作:

---
output:
  revealjs::revealjs_presentation:
    incremental: true
---
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $('div.chunk').children().each(function(i) {
    $(this).addClass('fragment');
  });
});
</script>

## Test

- 1
- 2

<div class="chunk">
```{r}
plot(cars)
```
</div>


## Test 2
<div class="chunk">
```{r}
head(mtcars)
```
</div>

首先加载jQuery框架。然后我们有一个简短的JS脚本,它找到所有带有类chunk的div容器。从那些我们带走所有孩子(源代码,输出或绘图)并将类fragment添加到他们。这将使它们逐渐显示。

最后将<div class="chunk">包裹在应该出现的所有那些块中。

当我将上面的所有JS代码(两个脚本元素)放在一个名为js.txt的文件中并使用self_contained: true以及includes: in_header: js.txt时,它可以正常工作并且也可以脱机。

---
output:
  revealjs::revealjs_presentation:
    incremental: true
    self_contained: true
    includes:
      in_header: js.txt
---