【问题标题】:Stop R Markdown from adding breaks between code and output阻止 R Markdown 在代码和输出之间添加中断
【发布时间】:2016-06-28 16:32:14
【问题描述】:

我正在使用以下 .rmd 代码从 R Markdown 创建一个 HTML 页面:

---
title: 'TITLE'
author: "NAME"
date: "DATE"
output: 
  html_document: 
    keep_md: yes
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
(n1 <- nrow(perf))  # Save the number of rows in 'perf'
(m1 <- ncol(perf))  # Save the number of columns in 'perf'
```

当我编织我的页面时,它会以单独的块返回我的代码和输出,但我希望它们都包含在一个块中,嵌入到我的 .md 中。结果如下所示:

但是,我希望部分之间的空格消失,并且代码和输出都包含在一个部分中。这可能吗?

我打算把它放在 GitHub 上,所以如果有任何方法可以使这个针对 GitHub 特定的降价工作会有所帮助(我知道这不是我当前配置 .md 的方式,但这是我的下一步)。

【问题讨论】:

    标签: r markdown r-markdown github-flavored-markdown


    【解决方案1】:

    非常简单,在 .rmd 文件中的代码块规范中包含规范 collapse = TRUE

    ```{r, collapse = TRUE}
    (n1 <- nrow(perf))  # Save the number of rows in 'perf'
    (m1 <- ncol(perf))  # Save the number of columns in 'perf'
    ```
    

    【讨论】:

      【解决方案2】:

      我有两种可能的解决方案:

      1.将每个源代码块与以下输出块结合起来

      您可以尝试在您的 RMarkdown 脚本中添加以下 JavaScript 块:

      <script>
      $chunks = $('pre.r');
      $chunks.each(function() {
          if ($(this).next().is('pre:not(r)')) {
              var newc = '<br>' + $(this).next('pre').children('code').html();
              var oldc = $(this).children('code').html();
              $(this).children('code').html(oldc.concat(newc));
              $(this).next('pre').remove();
          }
      })
      </script>
      

      2。将属于一个 R-chunk 的所有内容放入一个盒子中

      或者,如果您想将属于同一 RMarkdown 块的所有 html 块放在一个框中,请尝试

      <script>
      $chunks = $('pre.r');
      $chunks.each(function() {
          while($(this).next().is('pre')) {
              var newc = '<br>' + $(this).next('pre').children('code').html();
              var oldc = $(this).children('code').html();
              $(this).children('code').html(oldc.concat(newc));
              $(this).next('pre').remove();
          }
      })
      </script>
      

      【讨论】:

        猜你喜欢
        • 2021-10-03
        • 2019-04-13
        • 1970-01-01
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-03
        • 2021-06-07
        相关资源
        最近更新 更多