Compare two languages in R Markdown documents

A few months ago, I tried to compare two languages in an article. Here are several ways to do it.

Etienne Bacher
2020-12-03

In one of my first posts, I wanted to compare two languages (namely and R and Stata) by putting two chunks side-by-side. I asked how to do this on StackOverflow and continued to dig this question occasionally. I have now a few more or less convincing solutions/alternatives, that I summarize here.

Two code chunks side-by-side

It is possible to produce two columns in an HTML document (this is also possible for LaTeX files but the code is different). The following code…

:::: {style="display: grid; grid-template-columns: 70% 70%; grid-column-gap: 30px;"}

::: {}
```{r}
head(mtcars)
```
:::

::: {}
```{r}
head(anscombe)
```
:::

::::

… produces this output:

head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
head(anscombe)
  x1 x2 x3 x4   y1   y2    y3   y4
1 10 10 10  8 8.04 9.14  7.46 6.58
2  8  8  8  8 6.95 8.14  6.77 5.76
3 13 13 13  8 7.58 8.74 12.74 7.71
4  9  9  9  8 8.81 8.77  7.11 8.84
5 11 11 11  8 8.33 9.26  7.81 8.47
6 14 14 14  8 9.96 8.10  8.84 7.04

All of this is more detailed in this section of the R Markdown Cookbook.

Create tabs with {xaringanExtra}

An alternative to side-by-side chunks is to create tabs. We lose the ability to compare directly two chunks, but we can put much more tabs than code chunks. To do so, we use {xaringanExtra}, made by Garrick Aden-Buie. It is a great package that adds a lot of functionalities to R Markdown or {xaringan}.

To create tabs, we run xaringanExtra::use_panelset() first, and then we create the sections. Let’s init the panelset:

library(xaringanExtra)
# enable panelset
use_panelset()

Next, we can create several panels with ::::: {.panelset} and ::: {.panel}. Here’s an example:

::::: {.panelset}

::: {.panel}
[mtcars]{.panel-name}
```{r}
head(mtcars)
```
:::

::: {.panel}
[anscombe]{.panel-name}
```{r}
head(anscombe)
```
:::

::::
mtcars
head(mtcars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
anscombe
head(anscombe)
  x1 x2 x3 x4   y1   y2    y3   y4
1 10 10 10  8 8.04 9.14  7.46 6.58
2  8  8  8  8 6.95 8.14  6.77 5.76
3 13 13 13  8 7.58 8.74 12.74 7.71
4  9  9  9  8 8.81 8.77  7.11 8.84
5 11 11 11  8 8.33 9.26  7.81 8.47
6 14 14 14  8 9.96 8.10  8.84 7.04

Use <details>

Finally, it is also possible to create chunks that are hidden by default but can be expanded by the user. This is particularly useful if you want to provide the user a reference. For instance, if you’re trying to teach a new language, it might be helpful to provide code that the user already knows, without displaying it by default.

Here’s an example. We create the “expanding zone” below with:

<details>
<summary> Stata </summary>
Here, I put some Stata code hidden so that the user can compare if necessary:
```stata 
regress y x
```
</details>
# Here I'm teaching R
lm(mpg ~ drat, data = mtcars)
Stata

Here, I put some Stata code hidden so that the user can compare if necessary:

regress y x


That’s all! To summarize:

Corrections

If you see mistakes or want to suggest changes, please create an issue on the source repository.

Reuse

Text and figures are licensed under Creative Commons Attribution CC BY 4.0. Source code is available at https://github.com/etiennebacher/personal_website_distill, unless otherwise noted. The figures that have been reused from other sources don't fall under this license and can be recognized by a note in their caption: "Figure from ...".

Citation

For attribution, please cite this work as

Bacher (2020, Dec. 3). Etienne Bacher: Compare two languages in R Markdown documents. Retrieved from https://www.etiennebacher.com/posts/2020-12-03-code-two-columns-blogdown/

BibTeX citation

@misc{bacher2020compare,
  author = {Bacher, Etienne},
  title = {Etienne Bacher: Compare two languages in R Markdown documents},
  url = {https://www.etiennebacher.com/posts/2020-12-03-code-two-columns-blogdown/},
  year = {2020}
}