RMarkdown Reveal.js 演示代码折叠

标签 r r-markdown reveal.js presentation

我正在准备 RMarkdown Reveal.js 演示文稿。 我希望幻灯片中的 R 代码部分本质上是可折叠的。

我的 yaml header 如下所示,但代码折叠在最终演示文稿中不可见。

---
title: "Untitled"
output: 
  revealjs::revealjs_presentation:
    code_folding: hide

---


## R Markdown

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.


## Slide with R Code and Output

```{r}
summary(cars)
```

## Slide with Plot

```{r, echo=FALSE}
plot(cars)
```

添加 code_folding: hide 适用于常规 RMarkdown 文件。

code_folding 不能用于演示吗? 我应该尝试其他方法吗?

最佳答案

我摆弄了一些东西。我猜这仅适用于源代码块,但可以扩展到其他元素。大部分代码只是取 self 在上面评论中提到的答案。

完整的 MRE:

---
title: "Untitled"
output: 
  revealjs::revealjs_presentation:
    self_contained: true
---

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $chunks = $('div.sourceCode'); // get all divs containing source code...
  // add the button and a wrapping container to each of them...
  $chunks.each(function() {
    $(this).prepend('<div class=\"but_con\"><div class=\"showopt\">Show Source</div></div>');  // add the button and a wrapping container to each of them...
    $(this).find('code').toggle(); // hide them right away...
  });  

  // definition of the function to toggle visibility
  // we select all buttons, and add a click function
  $('.showopt').click(function() {
    var label = $(this).html();
    if (label.indexOf("Show") >= 0) {
      $(this).html(label.replace("Show", "Hide"));
    } else {
      $(this).html(label.replace("Hide", "Show"));
    }
    $(this).parent().siblings('pre').children('code').slideToggle('fast', 'swing');
  });

});
</script>


<style>
div.but_con {
  margin: auto;
  width: 90%;
  padding-bottom: 10px;
}

div.showopt {
  font-size: 35%;
  background-color: #004c93;
  color: #FFFFFF; 
  width: 100px;
  height: 20px;
  text-align: center;
  vertical-align: middle !important;
  float: right;
  font-family: sans-serif;
  border-radius: 8px;
  margin-bottom: 10px;
}

.showopt:hover {
    background-color: #dfe4f2;
    color: #004c93;
}
</style>

## R Markdown

This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.

When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.


## Slide with R Code and Output

```{r}
summary(cars)
```

## Slide with Plot

```{r, echo=FALSE}
plot(cars)
```

关于RMarkdown Reveal.js 演示代码折叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859074/

相关文章:

r - 通过匹配其他列来查找一列的唯一长度

r - 运行 R 脚本后无法杀死 worker

R Markdown、Knitr、Pandoc 和 Bookdown 之间的关系

r - 将迷你图添加到表中

r - 在 R 中使用集成()时的 "non-finite function value"

r - 如何在 RStudio Markdown 中编译 pdf?

r - 在同一 R Markdown 页面中将部分从两列更改为一列

css - 由 jupyter nbconvert 编译的 reveal.js 幻灯片可以在一张幻灯片上有两列吗?

html - revealjs 与 Rmarkdown - 设置字体和画线

python - 如何在 GitHub 页面上托管 iPython nbconvert 幻灯片