Compare two languages in R Markdown documents

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:

  • code chunks side-by-side
  • tabs
  • hidden chunks with <details>
Avatar
PhD Student in Economics

Related