R Markdown中单个块的代码折叠? [英] Code folding for individual chunks in R Markdown?

查看:264
本文介绍了R Markdown中单个块的代码折叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让代码折叠可用于R Markdown文档中的单个块,而不能用于其他块(不带

Is there a way to have code folding available for individual chunks in an R Markdown document, but not others (without writing customized JavaScript)?

我知道我可以使用code_folding YAML选项,但这适用于整个文档.我想为单个块而不是所有块启用它.

I know I can use the code_folding YAML option, but that applies to the whole document. I'd like to enable it for individual chunks, but not all chunks.

[原因是要编写一个实验室,其中包含不应隐藏的说明,但包含具有显示/隐藏解决方案的问题.]

[The reason is for writing a lab that contains instructions that should not be hideable, but with questions that have show/hide solutions.]

推荐答案

在rmarkdown 1.15之后

这已实现(请参阅相关的问题

After rmarkdown 1.15

This has been implemented (see related issue, PR and NEWS.md). However you should note that this folds only the code and not the output. You need to add some extra config to hide the code by default and not evaluate it.

---
title: "Bohemian Rhapcodey"
output: 
  html_document:
    code_folding: hide
---

## Question 1

Are you in love with your car?

```{r class.source = NULL, eval = FALSE}
summary(cars)
```

## Question 2

Are you under pressure?

```{r class.source = NULL, eval = FALSE}
plot(pressure)
```

在JSFiddle上尝试编织的HTML

此问题已于2019年7月在GitHub上关闭.使用html details 元素的解决方法.com/rstudio/rmarkdown/issues/664#issuecomment-422353592建议rel =" nofollow noreferrer> .

The issue was closed on july 2019 on GitHub. A workaround using the details element in html was suggested.

在实际使用之前,这可以在某些用例中起作用.

This can work for some use cases until it is actually implemented.

---
title: "Bohemian Rhapcodey"
output: html_document
---

## Question 1

Are you in love with your car?

<details>
  <summary>Toggle answer</summary>
  ```{r cars}
  summary(cars)
  ```
</details>

## Question 2

Are you under pressure?

<details>
  <summary>Toggle answer</summary>
  ```{r pressure}
  plot(pressure)
  ```
</details>

在JSFiddle上尝试编织的HTML

这篇关于R Markdown中单个块的代码折叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆