How to add dropdown menu on tab / tabset [rmarkdown / bootstrap]
Asked Answered
F

3

9

Documentation for Bootswatch suggests I can use a dropdown menu from a tab in a tabset:

enter image description here

How can I achieve this with Rmarkdown? I've tried:

# SECTION 1 {.tabset .tabset-fade}

## Section 1.1 

## Section 1.2 {????something here?????}
 ### Section 1.2.1  <<<<<<<<< want this to appear under the dropdown menu
Fairminded answered 13/5, 2017 at 18:6 Comment(3)
For now, I don't think this can be achieved using just rmarkdown. However, you can produce an HTML document with RMarkdown and then tweak the HTML to achieve a dropdown menu. I've filed a feature request issue on the RMarkdown github page which goes into more detail.Ninon
For now, you can either tweak the HTML to produce a document or use the bsselectR package which unfortunately seems to be in a somewhat-stalled development.Ninon
Thanks for opening the request @BIQS. I did not know I could do that. If you want to add the above as an answer, please do soFairminded
D
13

This is now available within the development version of rmarkdown, which you can install this via devtools::install_github("rstudio/rmarkdown"). To add a dropdown menu, you must add .tabset-dropdown to the class header as follows:

---
output: html_document
---

# Heading {.tabset .tabset-dropdown}

## Dropdown 1

## Dropdown 2

## Dropdown 3 

## Dropdown 4

enter image description here enter image description here

Dither answered 6/12, 2018 at 11:30 Comment(3)
thank you Michael, I have a problem with the arrow, do you know how to correct this : imgur.com/a/DxJO8xTGiblets
Same problem here!Accrete
same problem hereDasheen
N
6

For now, I don't think this can be done using just rmarkdown. However, you can produce an HTML document with a tabset section using rmarkdown and then tweak the HTML to convert the tab set to a dropdown menu. Alternatively, you can use the bsselectR package, which is unfortunately still in a somewhat-stalled development.

Below is an example of how you'd make an HTML document with rmarkdown and replace a tab set with a dropdown menu.

First, you'd write your rmarkdown document and then knit it to HTML.

---
title: "Tabset Example"
output: html_document
---

# The Tabset Section {.tabset .tabset-fade}

## First Tab
Here is the first tab's content.

## Second Tab
Here is the second tab's content
```

enter image description here

Then, in the resultant HTML file, you'd find this section of HTML:

<ul class="nav nav-tabs" role="tablist">
    <li role="presentation" class="active">
        <a role="tab" data-toggle="tab" href="#first-tab" aria-controls="first-tab">First Tab</a>
    </li>
    <li role="presentation">
        <a role="tab" data-toggle="tab" href="#second-tab" aria-controls="second-tab">Second Tab</a>
    </li>
</ul>

and replace it with this HTML:

 <ul class="nav nav-tabs" role="tablist">
     <li class="dropdown">
        <a class="dropdown-toggle" data-toggle="dropdown" href="#" aria-expanded="false">
          Choose a Tab <span class="caret"></span>
        </a>
        <ul class="dropdown-menu">
          <li class=""><a href="#first-tab" data-toggle="tab" aria-expanded="false" aria-controls="first-tab">First Tab</a></li>
          <li class=""><a href="#second-tab" data-toggle="tab" aria-expanded="false" aria-controls="second-tab">Second Tab</a></li>
        </ul>
     </li>
</ul>

Which should result in your tab set appearing as a dropdown menu, such as the following:

enter image description here

Ninon answered 14/8, 2017 at 15:8 Comment(0)
B
2

I got the same problem and I found this fantastic link. Changing the _site.yml as follows will help. But make sure - are inline, there is a space between - and text and click Tab for the - text under the menu:

name: my-website
output_dir: docs
navbar:
  title: My Website
  left:
  - text: Home
    href: index.html
  - text: Readings
    menu:
    - text: Module 1
      href: readings-module1.html
    - text: Module 2
      href: ./insert_a.pdf

enter image description here

Bedizen answered 5/4, 2022 at 8:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.