有没有一种方法可以让代码折叠在R Markdown文档中的单个块中可用,而不是其他块(没有writing customized JavaScript)?
我知道我可以使用code_folding YAML选项,但这适用于整个文档。我想为单个块启用它,但不是所有块。
原因是要编写一个实验,其中包含不应该隐藏的说明,但具有显示/隐藏解决方案的问题。
发布于 2018-12-20 22:18:38
rmarkdown 1.15之后
已经实现(参见相关issue、PR和NEWS.md)。但是,您应该注意到,这只折叠了代码,而不是输出。您需要添加一些额外的配置,以便在默认情况下隐藏代码,并且不对其进行评估。
---
title: "Bohemian Rhapcodey"
output:
html_document:
code_folding: hide
---
## Question 1
Are you in love with your car?
```{r class.source = NULL, eval = FALSE}摘要(汽车)
## Question 2
Are you under pressure?
```{r class.source = NULL, eval = FALSE}绘图(压力)
Try the knitted HTML on JSFiddle
rmarkdown 1.15之前的版本
The issue was closed on july 2019 on GitHub。在html was suggested中使用details元素的解决方法。
在实际实现之前,这可能适用于某些用例。
---
title: "Bohemian Rhapcodey"
output: html_document
---
## Question 1
Are you in love with your car?
<details>
<summary>Toggle answer</summary>
```{r cars}
summary(cars)问题2你有压力吗?
发布于 2019-06-19 06:15:37
使用details元素的变通方法工作得很好,但为了提高可发现性,我建议添加一些代码,以便用户在将鼠标悬停在该元素上时看到一个指向的手。这使得元素是交互式的变得更加明显。在7hibault的例子上进行扩展:
<style type="text/css">
details:hover { cursor: pointer }
</style>
---
title: "Bohemian Rhapcodey"
output: html_document
---
## Question 1
Are you in love with your car?
<details>
<summary>Toggle answer</summary>
```{r cars}
summary(cars)发布于 2020-09-02 16:34:32
使用rmarkdown版本2.3
基于7hibaut的答案,但并不完全相同。使用块标头中的选项class.source = "fold-show"来显示一个块,当其他块已经被YAML中的code_folding: hide隐藏时,如rmarkdown pull request中所述。
---
title: "Bohemian Rhapsody"
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)
```
## Question 3
suggested code
```{r class.source = "fold-show", eval = FALSE}
library(dplyr)
library(magrittr)
a <- dMeasure::dMeasure$new()
a$open_emr_db()
## active patients
kensington_clinicians <- a$UserConfig %>>%
filter(
grepl(
"Kensington",
purrr::map_chr(Location, function(x) paste(x, collapse = ", "))
# map_chr will create a 'collapsed' version of all the
# listed locations
)
)
```

https://stackoverflow.com/questions/42543431
复制相似问题