首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >R Markdown中单个块的代码折叠?

R Markdown中单个块的代码折叠?
EN

Stack Overflow用户
提问于 2017-03-02 06:36:27
回答 4查看 3.8K关注 0票数 31

有没有一种方法可以让代码折叠在R Markdown文档中的单个块中可用,而不是其他块(没有writing customized JavaScript)?

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

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

EN

回答 4

Stack Overflow用户

发布于 2018-12-20 22:18:38

rmarkdown 1.15之后

已经实现(参见相关issuePRNEWS.md)。但是,您应该注意到,这只折叠了代码,而不是输出。您需要添加一些额外的配置,以便在默认情况下隐藏代码,并且不对其进行评估。

代码语言:javascript
运行
复制
---
title: "Bohemian Rhapcodey"
output: 
  html_document:
    code_folding: hide
---

## Question 1

Are you in love with your car?

```{r class.source = NULL, eval = FALSE}

摘要(汽车)

代码语言:javascript
运行
复制
## Question 2

Are you under pressure?

```{r class.source = NULL, eval = FALSE}

绘图(压力)

代码语言:javascript
运行
复制

Try the knitted HTML on JSFiddle

rmarkdown 1.15之前的版本

The issue was closed on july 2019 on GitHub。在html was suggested中使用details元素的解决方法。

在实际实现之前,这可能适用于某些用例。

代码语言:javascript
运行
复制
---
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你有压力吗?

Try the knitted HTML on JSFiddle

票数 7
EN

Stack Overflow用户

发布于 2019-06-19 06:15:37

使用details元素的变通方法工作得很好,但为了提高可发现性,我建议添加一些代码,以便用户在将鼠标悬停在该元素上时看到一个指向的手。这使得元素是交互式的变得更加明显。在7hibault的例子上进行扩展:

代码语言:javascript
运行
复制
<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)
票数 2
EN

Stack Overflow用户

发布于 2020-09-02 16:34:32

使用rmarkdown版本2.3

基于7hibaut的答案,但并不完全相同。使用块标头中的选项class.source = "fold-show"来显示一个块,当其他块已经被YAML中的code_folding: hide隐藏时,如rmarkdown pull request中所述。

代码语言:javascript
运行
复制
    ---
    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
        )
    )
    ```

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42543431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档