Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display several code chunks in a concise way

Tags:

r

blogdown

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?

like image 644
bretauv Avatar asked Dec 28 '19 13:12

bretauv


People also ask

What is a chunk in coding?

A code chunk is used to output JS, PHP, HTML, etc. Include the code's opening and closing tags. For example, for Javascript, use <script></script> tags.

Where is the code chunk in r?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS).

What code chunk option setting prevents code but not the results of that code from appearing in the finished file?

Chunk Options include = FALSE prevents code and results from appearing in the finished file. R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures.


1 Answers

Since my previous answer was a bit messy and not very detailed, I make a new answer that is fine with what I wanted to do (i.e having the possibility to fold some code chunks).

The solution is to use Jonathan Sidi's details package, as pointed out by this answer. Originally, the aim of this package was to hide some outputs that take a lot of space, such as sessionInfo, in HTML documents made with R Markdown (and therefore with R Blogdown too). Here's an example (taken from the package's website):

---
title: "Test"
output: html_document
---

```{r}
library(details)

sessioninfo::session_info()%>%
  details::details(summary = 'current session info')
```

However, what I want to hide is not the output of some R code but a Stata code (that is not meant to be run), so that readers can compare R code to Stata code if they want to. Therefore, we must use some HTML to do so:

---
title: "Test"
output: html_document
---

## Regression with R and Stata

```{r, eval=FALSE}
lm(mpg ~ drat, data = mtcars)
```

<details>
<summary> Stata </summary>
```stata
regress y x
```
</details>

Now, readers can see the Stata code if they want to and those who don't want to are not distracted by too many code chunks. This works for Blogdown articles too (since it is "just" some R Markdown on a website).

More features (customize the hidden chunks for example) are detailed on the package's website.

like image 88
bretauv Avatar answered Oct 02 '22 20:10

bretauv