R Blogdown: display several code chunks in a concise way

I'm creating a blog with blogdown in which I compare code from R and code from Stata. I would like to show both codes so that the user can compare how it's done in R and in Stata. Howewer, putting two or more chunks in a row (code for R, code for Stata + output) makes the reading quite uncomfortable.

Several layouts came to my mind to include several chunks but I don't know if they are already implemented or if it's even possible to do so.

Have a button to display/hide chunks (one button per chunk)

One idea would be to have:

  • the R code chunks visible by default in the article,
  • the Stata code chunks invisible by default but visible if the user clicks on a button

This person and this person succeeded in folding their code chunks in blogdown but apparently it hides or shows every chunks by default. How can I hide only some chunks by default? Is there an option (like the options eval, echo...) that hides or shows code chunk in blogdown?

Chunks with "tabs"

The title of this part speaks for itself: is it possible to have tabs in a chunk so that we can switch from one code to the other (just like tabs in web browsers for example)?

Display two chunks side by side

In some blogdown themes (maybe all, I don't know), the width is quite reduced and there is some unused space on the sides. Therefore, is it possible to increase the width on some parts of an article and to display two chunks side by side?

Any idea if one of these layouts can be realized in blogdown?

This question has already been asked on StackOverflow and I only came up with a very partial answer (see in the same link) so I post it here.

I don't have answers for everything, but this package may be helpful to achieve the first of the 3 options (selectively hiding certain code blocks).

thanks, I will check this out

How's this for a starting point...

title: "Test"
output: html_document

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, error = TRUE)


# Foo {.tabset .tabset-fade .tabset-pills}

## R Code


## Stata Code

regress y x
1 Like

That is a great solution, much better that what I have done so far,

thanks a lot !

1 Like

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