sliderInput仅在选择了Tab时才显示在侧栏中

时间:2017-05-28 20:43:20

标签: shiny shinydashboard

我正在下面的模板中创建一个Shinyapp:

library(shinydashboard)

sidebar <- dashboardSidebar(
  sidebarMenu(id="tabs",
              menuItem("Tab1", tabName="Tab1", selected=TRUE),
              menuItem("Tab2", tabName = "Tab2")
  ),

  conditionalPanel("input.tabs=='Tab1'",
                   fluidRow()
                   ),
  conditionalPanel("input.tabs=='Tab2'",
                   fluidRow()
                   )
  )


body <- dashboardBody(
  tabItems(

    tabItem(tabName = "Tab1",
            fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
    ),
     tabItem(tabName = "Tab2",
           fluidRow(navbarPage(id = 'ab', title = "", 
                        tabPanel(title = "aa1", value = 'aa1', fluidRow()),
                        tabPanel(title = "aa2", value = 'aa2', fluidRow())))
    )))


ui = dashboardPage(
  dashboardHeader(title = "My tab"),
  sidebar,
  body
)

server = function(input, output) {}

shinyApp(ui = ui, server = server)

现在基本上我想要的是,只有来自TabPanel = 'aa2'的{​​{1}}时,才会在侧面板中显示sliderInput。因此,如果选择tabItem = 'Tab2',用户不应该看到该sliderInput。

到目前为止,我尝试过以下内容:

TabPanel = 'aa1'

然而,有了这个,sidebar <- dashboardSidebar( sidebarMenu(id="tabs", menuItem("Tab1", tabName="Tab1", selected=TRUE), menuItem("Tab2", tabName = "Tab2") ), conditionalPanel("input.tabs=='Tab1'", fluidRow() ), conditionalPanel("input.tabs=='Tab2'", fluidRow(sliderInput("aa5", "aa", value = 0.9, min = 0, max = 2, step=0.1)) ) ) 和&amp;都能看到sliderInput。 tabPanel = "aa1",我不想要。

任何建议,我如何才能使sliderInput仅对tabPanel = "aa2"可见。

感谢您的指针。

1 个答案:

答案 0 :(得分:2)

@SBista感谢指针。下面是更新的代码。谢谢,

library(shinydashboard)
library(shinyjs)

sidebar <- dashboardSidebar(
  sidebarMenu(id="tabs",
              menuItem("Tab1", tabName="Tab1", selected=TRUE),
              menuItem("Tab2", tabName = "Tab2")
  ),

  conditionalPanel("input.tabs=='Tab1'",
                   fluidRow()
                   ),
  conditionalPanel("input.tabs=='Tab2'",
                   fluidRow(useShinyjs(),
                    column(12, sliderInput("aa4", "aa", value = 0.9, min = 0, max = 2, step=0.1)))
                   )
  )


body <- dashboardBody(
  tabItems(

    tabItem(tabName = "Tab1",
            fluidRow( sliderInput("aa", "aa", value = 0.9, min = 0, max = 2, step=0.1))
    ),
     tabItem(tabName = "Tab2",
           fluidRow(navbarPage(id = 'ab', title = "", 
                        tabPanel(title = "aa1", value = 'aa1', fluidRow()),
                        tabPanel(title = "aa2", value = 'aa2', fluidRow())))
    )))


ui = dashboardPage(
  dashboardHeader(title = "My tab"),
  sidebar,
  body
)

server = function(input, output) {

observe(toggle(id = "aa4", condition = ifelse(input$ab == 'aa2', TRUE, FALSE)))

}

shinyApp(ui = ui, server = server)