我可以合并连续的代码块的代码,而不会在rmarkDown html_document中回荡

发布于 2025-02-13 11:57:48 字数 2136 浏览 0 评论 0原文

写入rmarkDown html_document我经常有连续的代码块,但没有输出。 当这种情况发生时,就像当

  • eval = false
  • 结果=“隐藏”

时,我希望代码块“合并”。 我知道ref.label =“”,它可以通过它来完成,但它会使块标签变得非常复杂/重。

是否有一些JavaScript,CSS,选项魔术可以帮助我实现自己想要的东西?

示例:

---                                               
title: "Test"                                     
date: '`r Sys.Date()`'                            
output: html_document                             
---                                               
                                                  
# Title 1                                         
                                                  
Some text                                         
                                                  
```{r, echo = TRUE, eval = FALSE}                 
1 + 1 # eval = FALSE so no output                 
```                                               
                                                  
```{r, echo = TRUE, results = "hide"}             
1 + 2 # results hidden so no output               
```                                               
                                                  
```{r, echo = TRUE}                               
1 + 3                                             
```                                               
                                                  
Some other text                                   
                                                  
```{r, echo = TRUE, eval = FALSE}                 
1 + 4 # eval = FALSE so no output but text follows
```                                               
                                                  
Some other text                                   
                                                  
```{r, echo = TRUE}                               
1 + 4                                             
``` 

输出

”在此处输入图像说明”

When writting rmarkdown html_document I often have successive code chunks that are displayed but have no outputs.
When this happen, like when

  • eval = FALSE
  • results = "hidden"

I would like the code chunks to be "merged".
I know about ref.label = "" and it could be done with it but it would makes chunk label to be extremely complex/heavy.

Is there some javascript, css, option magic to help me achieving what I'd like ?

example:

---                                               
title: "Test"                                     
date: '`r Sys.Date()`'                            
output: html_document                             
---                                               
                                                  
# Title 1                                         
                                                  
Some text                                         
                                                  
```{r, echo = TRUE, eval = FALSE}                 
1 + 1 # eval = FALSE so no output                 
```                                               
                                                  
```{r, echo = TRUE, results = "hide"}             
1 + 2 # results hidden so no output               
```                                               
                                                  
```{r, echo = TRUE}                               
1 + 3                                             
```                                               
                                                  
Some other text                                   
                                                  
```{r, echo = TRUE, eval = FALSE}                 
1 + 4 # eval = FALSE so no output but text follows
```                                               
                                                  
Some other text                                   
                                                  
```{r, echo = TRUE}                               
1 + 4                                             
``` 

output

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

木格 2025-02-20 11:57:49

一种方法是使用 lua filter 合并代码块:

---
title: "Test"
date: '`r Sys.Date()`'
output:
  html_document:
    pandoc_args:
      - '--lua-filter=merge-code-blocks.lua'
---

在哪里: Merge-code-blocks.lua包含

local code_block_sep = '\n\n'
function Blocks (blocks)
  for i = #blocks, 2, -1 do -- start at end of list
    -- Both blocks must be code blocks and share the same primary class
    if blocks[i - 1].t == 'CodeBlock' and
       blocks[i].t == 'CodeBlock' and
       blocks[i - 1].classes[1] == blocks[i].classes[1] then
      blocks[i - 1].text = blocks[i - 1].text ..
        code_block_sep ..
        blocks[i].text
      blocks:remove(i)
    end
  end
  return blocks
end

结果文档:

set code_block_sep ='\ n'如果您不喜欢将块分开的空白线。

这种方法的优点是它不是针对HTML的特定,即使使用PDF或单词输出也将继续使用。

One approach is to use a Lua filter to merge the code blocks:

---
title: "Test"
date: '`r Sys.Date()`'
output:
  html_document:
    pandoc_args:
      - '--lua-filter=merge-code-blocks.lua'
---

where merge-code-blocks.lua contains

local code_block_sep = '\n\n'
function Blocks (blocks)
  for i = #blocks, 2, -1 do -- start at end of list
    -- Both blocks must be code blocks and share the same primary class
    if blocks[i - 1].t == 'CodeBlock' and
       blocks[i].t == 'CodeBlock' and
       blocks[i - 1].classes[1] == blocks[i].classes[1] then
      blocks[i - 1].text = blocks[i - 1].text ..
        code_block_sep ..
        blocks[i].text
      blocks:remove(i)
    end
  end
  return blocks
end

Resulting document:

Screenshot of the resulting webpage

Set code_block_sep = '\n' if you don't like the blank line separating the blocks.

The advantage of this approach is that it is not specific to HTML and will continue to work even with PDF or Word output.

一抹微笑 2025-02-20 11:57:49

如果您需要的只是默认的HTML输出,那么您可以使用小型CSS攻击来获取所需的东西。它可以直接移动任何代码块,然后再向上移动1EM,卸下顶部边框并防止弯道被舍入。这给出了被合并的细胞的视觉效果。

必要的片段可以放置在文档中的任何地方。

<style>
  pre.r + pre.r {
    margin-top: -1em;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
    border-top: none;
  }
</style>

If all you need is the default HTML output, then you can use a small CSS hack to get what you want. It moves any code block directly following another upwards by 1em, removes the top border and prevents corners from being rounded. This gives the visual effect of the cells being merged.

The necessary snippet can be placed anywhere in the document.

<style>
  pre.r + pre.r {
    margin-top: -1em;
    border-top-left-radius: 0;
    border-top-right-radius: 0;
    border-top: none;
  }
</style>

Screenshot of the original webpage after applying the above CSS hack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文