statquant
statquant

Reputation: 14370

Can I merge the code of successive code chunks with no output echoed in a rmarkdown html_document

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

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

Upvotes: 8

Views: 156

Answers (2)

tarleb
tarleb

Reputation: 22609

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.

Upvotes: 6

tarleb
tarleb

Reputation: 22609

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

Upvotes: 3

Related Questions