根据用户在闪亮应用程序中的选择显示反应式tabsetPanel

时间:2019-08-22 13:55:06

标签: r shiny

我有一个闪亮的应用程序,其中有一个带有四个按钮的radioButtons小部件。当没有一个被单击时,我希望显示tabsetPanel "tabC"。如果选择了“关于”,则根本不需要tabsetPanel,如果选择了“ Section A,BC”,则希望显示tabsetPanel "tabA"

#ui.r
ui <- fluidPage(
    theme=shinytheme("slate") ,
    # App title ----
    titlePanel("Tabsets"),

    # Sidebar layout with input and output definitions ----
    sidebarLayout(

      # Sidebar panel for inputs ----
      sidebarPanel(
        uiOutput("rad")
      ),

      # Main panel for displaying outputs ----
      mainPanel(
       uiOutput("tabers")


      )
    )
  )
#server.r
library(shiny)

server = function(input, output) {

  output$rad<-renderUI({
    radioButtons("radio", label = "",
                 choices = list("About" = 1, "Sector A" = 2, "Sector B" = 3,"Sector C" = 4), 
                 selected = character(0))
  })


  output$tabers<-renderUI({
    if(input$radio=="Sector A"){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")

      )
    }
    else if(input$radio=="Sector B"){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")

      ) 
    }
    else if(input$radio=="Sector C"){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")

      ) 
    }
   else if(input$radio=="About"){

   }
    else{
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Performance" )

      ) 
    }

  })

}

2 个答案:

答案 0 :(得分:2)

我相信,由于您的选择具有数字值,因此您需要将input$radio与数字值进行比较,例如:if (input$radio == 2)用于A区。

此外,当未选择任何单选按钮时,input$radio应该为NULL。您可以在开始时进行检查,如果为NULL,请显示tabC。

请告诉我这是否具有所需的行为。

library(shiny)
library(shinythemes)

#ui.r
ui <- fluidPage(
  theme=shinytheme("slate") ,
  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      uiOutput("rad")
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      uiOutput("tabers")
    )
  )
)
#server.r

server = function(input, output) {

  output$rad<-renderUI({
    radioButtons("radio", label = "",
                 choices = list("About" = 1, "Sector A" = 2, "Sector B" = 3,"Sector C" = 4), 
                 selected = character(0))
  })

  output$tabers<-renderUI({
    if(is.null(input$radio)) {
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Performance")
      )
    }
    else if(input$radio==2){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")
      )
    }
    else if(input$radio==3){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")
      ) 
    }
    else if(input$radio==4){
      tabsetPanel(
        id="tabA",
        type = "tabs",
        tabPanel("Constituents"),
        tabPanel("Clusters" ),
        tabPanel("Index")
      ) 
    }
    else if(input$radio==1){

    }
    # Left last else in here but should not get called as is
    else{
      tabsetPanel(
        id="tabC",
        type = "tabs",
        tabPanel("Global"),
        tabPanel("Performance" )
      ) 
    }
  })
}

shinyApp(ui, server)

答案 1 :(得分:1)

请仔细考虑一下您的应用,使用shinydashboar软件包中的一些功能来查看此选项。

library(shiny)
library(shinydashboard)
library(shinythemes)

#ui.r
ui <- fluidPage(
  theme=shinytheme("slate") ,
  # App title ----
  titlePanel("Tabsets"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(
      sidebarMenu( id = "tab",
      menuItem("Home", tabName = "home"),
      menuItem("Sector A", tabName = "sect_a"),
      menuItem("Sector b", tabName = "sect_b"),
      menuItem("Sector c", tabName = "sect_c"),
      menuItem("About", tabName = "about")
      )
    ),

    # Main panel for displaying outputs ----
    mainPanel(
      tabItems(

        # Home tab
        tabItem(
          tabName = "home",
          tabsetPanel(
            id="tabC",
            type = "tabs",
            tabPanel("Global"),
            tabPanel("Performance" ))
        ),

        tabItem(
          tabName = "sect_a",
          tabsetPanel(
            id="tabA",
            type = "tabs",
            tabPanel("Constituents"),
            tabPanel("Clusters" ),
            tabPanel("Index"))
        ),

        tabItem(
          tabName = "sect_b",
          tabsetPanel(
            id="tabA",
            type = "tabs",
            tabPanel("Constituents"),
            tabPanel("Clusters" ),
            tabPanel("Index"))
        ),

        tabItem(
          tabName = "sect_c",
          tabsetPanel(
            id="tabA",
            type = "tabs",
            tabPanel("Constituents"),
            tabPanel("Clusters" ),
            tabPanel("Index"))),

        tabItem(tabName = "about")
      )




    )
  )
)

#server.r


server = function(input, output) {

}



shinyApp(ui, server)
相关问题