Search code examples
rr-markdown

Generating separate tabs for individual subsections


I was wondering if I could get some help.

Say I have 3 files:

top-level.rmd
q1.rmd
q2.rmd

The contents of top-level.rmd:

---
title: "hw1-2330981"
author: "Ian Quah"
date: "`r Sys.Date()`"
output: html_document
---


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


```{r child = 'q1.Rmd'}

```

```{r child = 'q2.Rmd'}

```

(apologies for the formatting coming out weird)

The contents of q1.rmd:

---
title: "hw_sol_q1"
author: "Ian Quah"
date: "`r Sys.Date()`"
output: html_document
---

# Question 1: `pressure.csv` {.tabset}

## a) `getwd()`

```{r }
getwd()
```

and the contents for q2.rmd

---
title: "hw_sol_q1"
author: "Ian Quah"
date: "`r Sys.Date()`"
output: html_document
---

# Question 2: something else {.tabset}

## a) `getwd()`

```{r }
getwd()
```

I am trying to generate two tabs on the main page, where each tab contains a corresponding question with all the sub-questions in a flat format (we scroll down to see the solutions for each subquestion). I've tried adding {.tabset} to the top-level header of each question (see below) but the end result isn't what I'm hoping for.

TL;DR I want


Top-level page with some text

|tab1| |tab2|

(with tab1 active)

Q1.1

. . .

Q1.2

and if I click on tab 2 I will then see

Q2.1

. . .

Q2.2

rendered on the top-level


Solution

  • You need to specify the .tabset class on the mother element of the tabs:

    top-level.Rmd

    ---
    title: "hw1-2330981"
    author: "Ian Quah"
    date: "`r Sys.Date()`"
    output: html_document
    ---
    
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    # Questions {.tabset}
    
    ```{r child = 'q1.Rmd'}
    ```
    
    ```{r child = 'q2.Rmd'}
    ```
    

    q1.Rmd

    ---
    title: "hw_sol_q1"
    author: "Ian Quah"
    date: "`r Sys.Date()`"
    output: html_document
    ---
    
    ## Question 1: `pressure.csv`
    
    ### a) `getwd()`
    
    ```{r}
    1 + 1
    ```
    

    q2.Rmd

    ---
    title: "hw_sol_q1"
    author: "Ian Quah"
    date: "`r Sys.Date()`"
    output: html_document
    ---
    
    ## Question 2: something else 
    
    ### a) `getwd()`
    
    ```{r}
    1 + 1
    ```
    

    Output

    Rendered RMarkdown report with two tabs showing the separate questions


    Additional Remark

    If for some reason you do not like to show the (somewhat superfluous) header labeled Questions, you can use some custom CSS to hide it altogether:

    ```{css, echo = FALSE}
    .questions > h1 {
      display: none;
    }
    ```
    
    # Questions {.tabset .questions}