Search code examples
rr-markdownbookdown

Using {.tabset} with bookdown::bs4_book()


I'm creating a {bookdown} project using the new bookdown::bs4_book() output. However, when I try to add tabs to a chapter using {.tabset} as described here, the tabs are not generated. Instead, the document is rendered in the normal linear layout.

Is it possible to use {.tabset} with bookdown::bs4_book()?

I've made a minimal bs4_book() project in this GitHub repo, and the rendered output is hosted here: https://bs4-reprex.netlify.app/intro.html

Here is the raw R Markdown code for the linked chapter (also available in the repo here).

# Introduction {#intro}

You can label chapter and section titles using `{#label}` after them, e.g., we can reference Chapter \@ref(intro).
If you do not manually label them, there will be automatic labels anyway, e.g., Chapter \@ref(methods).

## Figures and Tables {.tabset}

### Figures

Figures and tables with captions will be placed in `figure` and `table` environments, respectively.

```{r nice-fig, fig.cap='Here is a nice figure!', out.width='80%', fig.asp=.75, fig.align='center'}
par(mar = c(4, 4, .1, .1))
plot(pressure, type = 'b', pch = 19)
```

### Tables

Reference a figure by its code chunk label with the `fig:` prefix, e.g., see Figure \@ref(fig:nice-fig).
Similarly, you can reference tables generated from `knitr::kable()`, e.g., see Table \@ref(tab:nice-tab).

```{r nice-tab, tidy=FALSE}
knitr::kable(
  head(iris, 20), caption = 'Here is a nice table!',
  booktabs = TRUE
)
```

##  {.unnumbered}

You can write citations, too.
For example, we are using the **bookdown** package [@R-bookdown] in this sample book, which was built on top of R Markdown and **knitr** [@xie2015].

Solution

  • From this response, .tabset is not a supported feature of {bookdown}. However, this can still be achieved using xaringanExtra::use_panelset().

    ```{r panel-setup, include = FALSE}
    xaringanExtra::use_panelset()
    xaringanExtra::style_panelset(font_family = "inherit")
    ```
    
    # Introduction {#intro}
    
    You can label chapter and section titles using `{#label}` after them, e.g., we can reference Chapter \@ref(intro).
    If you do not manually label them, there will be automatic labels anyway, e.g., Chapter \@ref(methods).
    
    ## Figures and Tables {.panelset}
    
    ### Figures
    
    Figures and tables with captions will be placed in `figure` and `table` environments, respectively.
    
    ```{r nice-fig, fig.cap='Here is a nice figure!', out.width='80%', fig.asp=.75, fig.align='center'}
    par(mar = c(4, 4, .1, .1))
    plot(pressure, type = 'b', pch = 19)
    ```
    
    ### Tables
    
    Reference a figure by its code chunk label with the `fig:` prefix, e.g., see Figure \@ref(fig:nice-fig).
    Similarly, you can reference tables generated from `knitr::kable()`, e.g., see Table \@ref(tab:nice-tab).
    
    ```{r nice-tab, tidy=FALSE}
    knitr::kable(
      head(iris, 20), caption = 'Here is a nice table!',
      booktabs = TRUE
    )
    ```
    
    ##  {.unnumbered}
    
    You can write citations, too.
    For example, we are using the **bookdown** package [@R-bookdown] in this sample book, which was built on top of R Markdown and **knitr** [@xie2015].
    

    {xaringanExtra} isn't on CRAN yet, but can be installed from GitHub with remotes::install_github("gadenbuie/xaringanExtra"). Also note that pansel sets are not portable to non-HTML bookdown outputs (e.g., PDF), which is why tabset is not natively supported in bookdown.