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

前端 未结 2 849
小蘑菇
小蘑菇 2021-02-10 00:47

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

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

# SE         


        
相关标签:
2条回答
  • 2021-02-10 01:26

    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
    ```
    

    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:

    0 讨论(0)
  • 2021-02-10 01:31

    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
    

    0 讨论(0)
提交回复
热议问题