Display results of code chunk really 'asis' in distill (not within <div>)

Hi,

I'd like to insert results of code chunks in a distill_article 'asis', i.e. just the raw markdown code without any formatting or wrapping (s. Rmarkdown cookbook
). What works fine with rmarkdown::html_document I cannot achieve with distill::distill_articlewhere the results of code chunks seem to be always wrapped in <div> even using chunk option results='asis'.

I try to show this in the following reprex - I use

  • R v4.1.1
  • RStudio v1.4.1717,
  • distill v1.2
  • rmarkdown v2.10

The following Rmd file contains one chunk using results='asis' and can be rendered as html_document or distill_article.

---
title: "Test chunk option `results='asis'`"
output: 
  rmarkdown::html_document:
    keep_md: TRUE
  distill::distill_article:
    keep_md: TRUE
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r results='asis'}
cat("Test")
```

1) rmarkdown::html_document
The output from rmarkdown::html_document is as expected where the result is rendered correctly as just Test in the resulting (intermediate) markdown File (I leave out the YAML here):

Test

2) distill::distill_article
Using output distill::distill_article the option results='asis' does lead to a raw markdown output: In the rendered md-File the result of the code chunk is still wrapped in a <div>., s. the markdown file.


<div class="layout-chunk" data-layout="l-body">
Test

</div>

```{.r .distill-force-highlighting-css}
```

Is there a way to achieve raw markdown output from code chunks in distill, not wrapped in a <div>?

Thanks for your time!
Best, G

No I don't think there is. All chunk outputs gets rendered in the intermediate .md file with those div. This is part of the distill format.

Look at the output of

---
title: "Test chunk option `results='asis'`"
output: 
  distill::distill_article:
    keep_md: TRUE
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
1+1
```

```{r results='asis'}
cat("Test")
```

What is the use case that you have needing that ?
Maybe with result='asis' we could not included the div, but this type of change needs to be backed by a problem to solve.

If you have one to share, you can open a feature request on the Github repository

Thank you !

Hi Christophe,

thanks for your response and for your work on distill! I indeed have a use case for a version of results = 'asis' that does not wrap the results into divs.

I would like to use panelsets in distill articles using xaringanExtra (https://github.com/gadenbuie/xaringanExtra#-panelset.) and programmatically create a number of panelsets (for multiple variables of a data frame), the panels filled with the results of multiple code chunks (plots, tables, etc.). Since panelsets work in distill only by using fenced divs, I'd insert these from within the code chunks using cat(), (e.g. cat("::::: {.panelset}", "\n")) and using chunk option results = 'asis' . Some code chunks to illustrate this:

The following code works fine in distill since the fenced divs and all the content for the tabs are integrated in one single code chunk, so there is no problem since all the output is wrapped in one surrounding <div class="layout-chunk" data-layout="l-body"> results from the code chunk </div>.

```{r results='asis'}
library(xaringanExtra)
xaringanExtra::use_panelset()

cat("::::: {.panelset}", "\n")
cat("::: {.panel}", "\n", "[First Tab]{.panel-name}", "\n")
plot(rnorm(50), main = "Plot 1")
cat(":::", "\n")
cat("::: {.panel}", "\n", "[Second Tab]{.panel-name}", "\n")
plot(rnorm(150), main = "Plot 2")
cat(":::", "\n")
cat(":::::", "\n")

If we split the same code into multiple code chunks the panelsets do not work anymore, I guess because the div that opens for starting the panelset does not wrap the entire output, s. the following chunks:

```{r results='asis'}
cat("::::: {.panelset}", "\n")
cat("::: {.panel}", "\n", "[First Tab]{.panel-name}", "\n")
```{r results='asis'}
plot(rnorm(50), main = "Plot 1")
cat(":::", "\n")
```{r results='asis'}
cat("::: {.panel}", "\n", "[Second Tab]{.panel-name}", "\n")
plot(rnorm(150), main = "Plot 2")
cat(":::", "\n")
cat(":::::", "\n")

Since I want to insert panelsets programmatically via loop (with plots and tabs for a varying number of variables from a df), I would need a results='asis' option for distill that really inserts the results as they are, without any wrapping. Hope I could make it a little clearer why this would be of help. I'll open a feature request on the Github repo.

Thanks a lot!

Ok I understand better.

However:

  • I fear this div is important for distill layout. It is possible that removing it could cause other issues with distill styling or behaving.
  • I understand it is working when you provide the code in a single code chunk. That is great ! and that makes sense to do so. Why divide in several chunks then ?
  • When using panelset with R Markdown, it is advised to use fenced div but you could also use HTML directly. Either written through your loop or using htmltools probably.

I saw you open an issue: https://github.com/rstudio/distill/issues/405

No need to duplicate questions - and at least, it is good practice to cross reference.

Thanks

Hi Christophe,

thanks for your response and your considerations!

  • I understand that this div is important for the distill layout , but I think a version of result = 'asis' without divs, i.e. where the divs can be set manually could be of value also for other situations where the automatic wrapping could cause problems (in form of another chunk option, let's call it result.div = FALSE that would only be relevant for output formats (like distill) in which the chunk outputs are wrapped) .
  • I'd like to structure the code into multiple chunks because of multiple plots (with different sizes and captions, etc.). Putting all the code in one single chunk and put the chunk options in vectors could work, but might be cumbersome when there are changes.
  • I think that inserting the HTML directly as mentioned is only a solution here after unwrapping the content first. Then we could insert the divs manually and create <div class="layout-chunk" data-layout="l-body"> results from multiple code chunks </div>

Thanks a lot for your time!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.