【问题标题】:Code folding for individual chunks in R Markdown?R Markdown中单个块的代码折叠?
【发布时间】:2017-03-01 22:36:27
【问题描述】:

有没有办法让代码折叠可用于 R Markdown 文档中的单个块,但不能用于其他块(没有 writing customized JavaScript)?

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

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

【问题讨论】:

标签: r knitr r-markdown


【解决方案1】:

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}
summary(cars)
```

## Question 2

Are you under pressure?

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

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)
  ```
</details>

## Question 2

Are you under pressure?

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

Try the knitted HTML on JSFiddle

【讨论】:

  • OP 想要 individual 块的代码折叠,他们已经知道 code_folding yaml 选项
  • @HongOoi 如果您不相信,请重新阅读答案并检查提供的链接和针织 html。 code_folding 选项现在也适用于单个块。 OP 在 2017 年意识到 code_folding 选项时还没有单独的代码折叠,未来的读者可能会发现知道它现在得到官方支持很有用。
  • 我相信knitr 单元格中的class.source 参数应该等于"fold-show",如bookdown 中所述,而不是等于NULL
【解决方案2】:

使用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)
  ```
</details>

【讨论】:

    【解决方案3】:

    使用rmarkdown 2.3 版

    基于 7hibaut 的回答,但不完全相同。在 YAML 中使用 code_folding: hide 隐藏其他块时,使用块头中的选项 class.source = "fold-show" 来显示块,如 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
            )
        )
        ```
    

    【讨论】:

      【解决方案4】:

      当code_folding: hide 并且您想要显示选择代码块时,我会这样做:

      ```
      5 * 5
      ```
      ```{r}
      5 * 5 
      ```
      

      第一个块(没有{r})只是显示代码,第二个块运行代码

      【讨论】:

        猜你喜欢
        • 2017-02-21
        • 1970-01-01
        • 2019-02-12
        • 1970-01-01
        • 2016-02-02
        • 1970-01-01
        • 1970-01-01
        • 2012-08-29
        • 1970-01-01
        相关资源
        最近更新 更多