Dynamically generate tabPanels for an existing tabsetPanel

866 views
Skip to first unread message

ma...@roundtableanalytics.com

unread,
Apr 7, 2017, 8:37:34 PM4/7/17
to Shiny - Web Framework for R
I'm trying to dynamically build a tabsetPanel.  The following code seems like it should work but doesn't build the tabPanel elements correctly.  Is there another way to accomplish this? 


I'm trying this because I also have a situation where I have an existing tabsetPanel with some tabPanels in it and I want to dynamically add more tabPanels and I'm brainstorming how I can accomplish this without the use of javascript.


----------------------------------------------------------------

library(shiny)

ui <- fluidPage(
   div(actionButton("trial2", "Add tabset panel - trial 2")),
   tags$div(id = "placeholder2")
)

server <- function(input, output, session) {

  observeEvent(input$trial2, {

    mytabs = 
        tabsetPanel(id='tabs2',
            lapply(1:2, function(x){
                tabPanel(id=paste0('tab2',x), value=paste0('value',x), title=paste0("Tab2 ", x),
                   fluidRow(
                          div(id=paste0("div2",x), 
                                 checkboxGroupInput(
                                        paste0("checkbox2", x) ,
                                       "Choose",
                                       choices = c('One','Two','Three'),
                                       inline = TRUE
                             ))
                      )
                 )
             })
           )

    print(mytabs)

    insertUI(selector = '#placeholder2', where = "afterBegin", mytabs)

  })

}

shinyApp(ui = ui, server = server)

----------------------------------------------------------------

The output of mytabs is:


<div class="tabbable">

    <ul class="nav nav-tabs shiny-tab-input" id="tabs2">

        <li class="active">

            <a href="#tab-9514-1" data-toggle="tab"></a>

        </li>

    </ul>

    <div class="tab-content">

        <div class="tab-pane" title="Tab2 1" data-value="value1" id="tab21">

            <div class="row">

                <div id="div21">

                    <div id="checkbox21" class="form-group shiny-input-checkboxgroup shiny-input-container shiny-input-container-inline">

                        <label class="control-label" for="checkbox21">Choose</label>

                        <div class="shiny-options-group">

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox21" value="One" />

                                <span>One</span>

                            </label>

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox21" value="Two" />

                                <span>Two</span>

                            </label>

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox21" value="Three" />

                                <span>Three</span>

                            </label>

                        </div>

                    </div>

                </div>

            </div>

        </div>

        <div class="tab-pane" title="Tab2 2" data-value="value2" id="tab22">

            <div class="row">

                <div id="div22">

                    <div id="checkbox22" class="form-group shiny-input-checkboxgroup shiny-input-container shiny-input-container-inline">

                        <label class="control-label" for="checkbox22">Choose</label>

                        <div class="shiny-options-group">

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox22" value="One" />

                                <span>One</span>

                            </label>

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox22" value="Two" />

                                <span>Two</span>

                            </label>

                            <label class="checkbox-inline">

                                <input type="checkbox" name="checkbox22" value="Three" />

                                <span>Three</span>

                            </label>

                        </div>

                    </div>

                </div>

            </div>

        </div>

        tab-9514-1 tab-pane active

    </div>

</div>

Joe Cheng

unread,
Apr 7, 2017, 8:43:32 PM4/7/17
to ma...@roundtableanalytics.com, Shiny - Web Framework for R
You can dynamically build a tabsetPanel using do.call (e.g. do.call(tabsetPanel, listOfTabPanels)). But that doesn't help with the problem of dynamically adding new tabs to an existing tabset.

It's kind of embarrassing that this isn't in Shiny base, but it's not (unless I am forgetting something we did recently--but I don't think I am).

These Stack Overflow threads may help you:

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/a79b16d1-cfb8-41d1-92c8-d3a177a66c26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages