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

Time:07-07

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

CodePudding user response:

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

CodePudding user response:

One approach is to use a 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.

  • Related