Mohan Raj
Mohan Raj

Reputation: 77

Toggle option in shiny

In my shiny dashboard, I have a radio button as follows,

radioButtons("view", "View Type", c("Count", "Percent"), selected = "Count")

Now based on this radio button selection I want my output tabs to be toggled based upon the selection. What is mean is that if i select "Count" then the "Count" view should be shown and "Percent" view should be disabled.

Below, is the code for my main tab,

mainPanel(width = 9,
   tabsetPanel("tab",
   tabPanel(strong("Count of Objects"), tableOutput("mon")),
   tabPanel(strong("% of Objects"), tableOutput("mon_per")))

Any help is appreciated.

Upvotes: 2

Views: 6200

Answers (1)

Valter Beaković
Valter Beaković

Reputation: 3250

You can use the updateTabsetPanel() function. This is the code:

ui.R

    library(shiny)


    shinyUI(pageWithSidebar(


            headerPanel("Tabsets"),
            sidebarPanel(
                    radioButtons("view", "View Type:",
                                 c("Count", "Percent"), 
                                 selected = "Count")

            ),
            mainPanel(
                    tabsetPanel(id = "tab",
                                tabPanel(title = "Count of objects", 
                                         value = "countsOfObjects", 
                                         tableOutput("mon")),
                                tabPanel(title = "% of Objects", 
                                         value = "percentOfObjects", 
                                         tableOutput("mon_per")))

                    )
            )
    )

server.R

    library(shiny)

    shinyServer(function(input, output, session) {
            observeEvent(input$view, {
                    if (input$view == "Count") {
                            updateTabsetPanel(session, "tab",
                                              selected = "countsOfObjects"
                            )      
                    } else {
                            updateTabsetPanel(session, "tab",
                                              selected = "percentOfObjects"
                            )                         
                    }

            })

    })

Upvotes: 4

Related Questions