Put 2 chunks of code side by side in RMarkdown or Quarto
Asked Answered
C

2

5

How can I put 2 chunks of code side by side in the output file of RMarkdown or Quarto ?

Code

library(dplyr)
mtcars %>% select(gear)
library(dplyr)
select(mtcars, gear)

Desired layout in the PDF or HTML file enter image description here

Carrack answered 29/7, 2022 at 12:32 Comment(0)
B
3
---
title: "Untitled"
output: html_document
---

:::::::::::::: {.columns}
::: {.column width="50%"}


```{r warning=FALSE,message=FALSE}
library(dplyr)
mtcars %>% select(gear)
```

:::
::: {.column width="50%"}

```{r warning=FALSE,message=FALSE}
library(dplyr)
select(mtcars, gear)
```

:::
::::::::::::::

enter image description here

used This SO question as a resource. This is using pandoc to format the document in Rmarkdown HTML output

Bohaty answered 29/7, 2022 at 20:28 Comment(3)
It outputs one column on my rendered PDF.Carrack
Ah ok, it works for HTMLCarrack
Yes, then if you NEED to use PDF, output Rmarkdown to html_document and then view the HTML file in your web browser, then "Print to PDF" whenever needed to retain the best HTML styleBohaty
C
5

The canonical way for something like this is to use column divs:

::::: columns
::: column
```r
library(dplyr)
mtcars %>% select(gear)
```
:::

::: column
```r
library(dplyr)
select(mtcars, gear)
```
:::
:::::

This will work with HTML, reveal.js, Beamer, and Powerpoint. The default result looks a bit ugly in HTML, as there is no space between the two blocks, but we can fix that with a tiny bit of CSS. We can put it directly into the document:

<style>
.column { padding-right: 1ex }
.column + .column { padding-left: 1ex }
</style>

Things get more complicated if we wish to do the same for PDF. We'll need convert the divs into a table, as that's the most effective way to get elements side-by-side. But that requires some heavier tools. In the YAML header, add

output:
  pdf_document:
    pandoc_args:
      - "--lua-filter=columns-to-table.lua"

Then save the below code into a file column-to-table.lua.

function Div (div)
  if div.classes:includes 'columns' then
    local columns = div.content
      :filter(function (x)
        return x.classes and x.classes[1] == 'column'
      end)
      :map(function (x)
        return x.content
      end)
    local aligns = {}
    local widths = {}
    local headers = {}
    for i, k in ipairs(columns) do
      aligns[i] = 'AlignDefault'
      widths[i] = 0.98/ #columns
    end
    return pandoc.utils.from_simple_table(
      pandoc.SimpleTable('', aligns, widths, headers, {columns})
    )
  end
end

You can get rid of the lines around the table by adding

\renewcommand\toprule[2]\relax
\renewcommand\bottomrule[2]\relax

at the beginning of your document.

Countersink answered 29/7, 2022 at 21:1 Comment(3)
Hi there. Did anyone get this going for pdf? When I try it, the pdf is still in one column layout.Ungainly
@Ungainly If you want the full PDF to have two columns, then try with classoptions: twocolumn.Countersink
Thanks @tarleb. I played around with this option. My problem/task is to switch between onecolumn and twocolumn several times. The Latex-switch-options introduce an unwanted pagebreak when switching ... thus, i hoped your lua-filter based solution would work :). I also tried the recipe from the Rmarkdown cookbook bookdown.org/yihui/rmarkdown-cookbook/multi-column.html - That sort of works, however, I am fishing for a more elegant solution.Ungainly
B
3
---
title: "Untitled"
output: html_document
---

:::::::::::::: {.columns}
::: {.column width="50%"}


```{r warning=FALSE,message=FALSE}
library(dplyr)
mtcars %>% select(gear)
```

:::
::: {.column width="50%"}

```{r warning=FALSE,message=FALSE}
library(dplyr)
select(mtcars, gear)
```

:::
::::::::::::::

enter image description here

used This SO question as a resource. This is using pandoc to format the document in Rmarkdown HTML output

Bohaty answered 29/7, 2022 at 20:28 Comment(3)
It outputs one column on my rendered PDF.Carrack
Ah ok, it works for HTMLCarrack
Yes, then if you NEED to use PDF, output Rmarkdown to html_document and then view the HTML file in your web browser, then "Print to PDF" whenever needed to retain the best HTML styleBohaty

© 2022 - 2024 — McMap. All rights reserved.