根据选定的选项卡面板显示边栏内容

时间:2019-05-16 13:27:19

标签: r shiny shinydashboard

我有一个闪亮的仪表板,在侧边栏中有两个滑块,在主体中有两个选项卡面板。我要实现的是选择“ slider1”选项卡面板时仅显示“ slider1”,而选择“ slider2”选项卡面板时仅显示“ slider2”。

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    uiOutput("slider1"),
    uiOutput("slider2")
  ),
  dashboardBody(
    tabsetPanel(
      id = 'testingDPEtab',
      tabPanel("slider1"
      ),
      tabPanel("slider2"
      )
    )
  )
)

server <- function(input, output) {
  output$slider1<-renderUI({
    sliderInput("slider1", label = h3("Slider1"), min = 0, 
                max = 100, value = 50)
  })
  output$slider2<-renderUI({
    sliderInput("slider2", label = h3("Slider2"), min = 0, 
                max = 200, value = 50)
  })
}

shinyApp(ui, server)

2 个答案:

答案 0 :(得分:1)

编辑:

这是使用ShinyJS的可行解决方案:

library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    useShinyjs(),
    uiOutput("slider1"),
    uiOutput("slider2")
  ),
  dashboardBody(
    tabsetPanel(
      id = 'testingDPEtab',
      tabPanel("slider1Tab"),
      tabPanel("slider2Tab")
    )
  )
)

server <- function(input, output) {
  shinyjs::hide(id="slider1")
  shinyjs::hide(id="slider2")
  output$slider1<-renderUI({
    sliderInput("slider1", label = h3("Slider1"), min = 0,
                max = 100, value = 50)
  })
  output$slider2<-renderUI({
    sliderInput("slider2", label = h3("Slider2"), min = 0,
                max = 200, value = 50)
  })

  observe({
    if(input$testingDPEtab == "slider1Tab"){
      shinyjs::show(id="slider1")
      shinyjs::hide(id="slider2")
    } else {
      shinyjs::hide(id="slider1")
      shinyjs::show(id="slider2")
    }
  })
}

shinyApp(ui, server)

如果您想保留滑块(即隐藏它)而不是重新渲染它,则可能会更好。


原始帖子

我本来建议使用shinyJs,但我无法使其正常工作-也许是因为滑块是在服务器端而不是在UI中设置的?

因此,我采取了一种替代方法,将您的2个renderUI函数调用包装在一个observe()事件中。我使用if / else语句根据所选选项卡将一个控件设置为打开,将第二个控件设置为关闭。似乎工作正常。

library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    uiOutput("slider1"),
    uiOutput("slider2")
  ),
  dashboardBody(
    tabsetPanel(
      id = 'testingDPEtab',
      tabPanel("slider1Tab"),
      tabPanel("slider2Tab")
    )
  )
)

server <- function(input, output) {
  observe({
    if(input$testingDPEtab == "slider1Tab"){
      output$slider1<-renderUI({
        sliderInput("slider1", label = h3("Slider1"), min = 0, 
                    max = 100, value = 50)
      })
      output$slider2<-NULL
    } else {
      output$slider1<-NULL
      output$slider2<-renderUI({
        sliderInput("slider2", label = h3("Slider2"), min = 0, 
                    max = 200, value = 50)
      })
    }
  })
}

shinyApp(ui, server)

答案 1 :(得分:1)

如果没有必要,我通常会尝试避免呈现UI元素。

这是使用conditionalPanels的解决方案。

library(shiny)
library(shinydashboard)

shinyApp(

ui = dashboardPage(
    dashboardHeader(),

    dashboardSidebar(
        conditionalPanel(
            condition = "input.tabselected == 1",
            sliderInput("slider1", label = h3("Slider1"), min = 0, 
                    max = 100, value = 50)),
        conditionalPanel(
            condition = "input.tabselected == 2",
        sliderInput("slider2", label = h3("Slider2"), min = 0, 
                    max = 200, value = 50))
    ),
    dashboardBody(
        tabsetPanel(
            id = "tabselected",
            tabPanel("slider1", value = 1
            ),
            tabPanel("slider2", value = 2
            )
        )
    )
),

server = function(input, output) {

}
)