简体   繁体   English

Shiny 和 shinydashboard:如何仅在某些选项卡上显示侧边栏中的输入?

[英]Shiny and shinydashboard: How to display inputs in sidebar only on certain tabs?

I want to display inputs (checkboxes, select input) in the sidebar of my shiny dashboard, but only when a certain tab is clicked.我想在我的 shiny 仪表板的侧栏中显示输入(复选框,select 输入),但仅在单击某个选项卡时。

Minimum reporducible example below.下面的最小可重现示例。 How can I get the checkboxes and select input to only show up when on Page 2?如何让复选框和 select 输入仅在第 2 页上显示?

#ui.R

library(shiny)
library(shinydashboard)

# Define UI for application that draws a histogram
shinyUI(dashboardPage(
    dashboardHeader(title = "Test Application",
                    titleWidth = "400px"
    ),

    dashboardSidebar(
        id = "navbar",
        menuItem("Page 1", tabName = "page1"),
        menuItem("Page 2", tabName = "page2"),

        # THESE SHOW UP ALL THE TIME - HOW TO GET JUST ON PAGE 2?
        checkboxGroupInput("outcome", "Select Outcome Variable(s):", choices = c("Box 1", "Box 2", "Box 3")),
        selectInput("selectinput", label = "Select:", choices = c("Choice 1", "Choice 2", "Choice 2"))


    ),

    dashboardBody(
        tabItems(
            tabItem(
                tabName = "page1",
                h1("This is page 1")
            ),

            tabItem(
                tabName = "page2",
                h1("This is page 2")
            )
        )
    )
))

I assume something is needed in here to make the inputs dynamic?我认为这里需要一些东西来使输入动态化?

# server.R

library(shiny)

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
})

ANSWER: use a conditional panel that queries the selected tab.答案:使用查询选定选项卡的条件面板。

Credit: Mine at Rstudio信用:我在Rstudio

library(shiny)
library(shinydashboard)

# ui ---------------------------------------------------------------------------

ui <- dashboardPage(

  # title ----
  dashboardHeader(title = "Test Application"),

  # sidebar ----
  dashboardSidebar(
    sidebarMenu(id = "sidebarid",
                menuItem("Page 1", tabName = "page1"),
                menuItem("Page 2", tabName = "page2"),
                conditionalPanel(
                  'input.sidebarid == "page2"',
                  sliderInput("bins", "Number of bins:", min = 1, max = 50, value = 30),
                  selectInput("title", "Select plot title:", choices = c("Hist of x", "Histogram of x"))
                )
    )
  ),

  # body ----
  dashboardBody(
    tabItems(
      # page 1 ----
      tabItem(tabName = "page1", "Page 1 content. This page doesn't have any sidebar menu items."),
      # page 2 ----
      tabItem(tabName = "page2", 
              "Page 2 content. This page has sidebar meny items that are used in the plot below.",
              br(), br(),
              plotOutput("distPlot"))
    )
  )
)

# server -----------------------------------------------------------------------

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

  output$distPlot <- renderPlot({
    x    <- faithful[, 2]
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
    hist(x, breaks = bins, col = "darkgray", border = "white", main = input$title)
  })

}

# shiny app --------------------------------------------------------------------

shinyApp(ui, server)

If I understood your question correctly, all you need to do is the following: When defining the dashboardSidebar if you just want it as a navigational panel add sidebarmenu() and then add your menu items.如果我正确理解了您的问题,那么您需要做的就是:在定义dashboardSidebar时,如果您只想将其用作导航面板,请添加sidebarmenu() ,然后添加您的菜单项。

Then to have the checkboxes and select input appear only for the main dashboard on page 2 add it under dashboardbody() with the tabItem = "page2" .然后让复选框和 select 输入仅出现在第 2 页的主仪表板中,将其添加到dashboardbody()下,并带有tabItem = "page2" See below:见下文:

#ui.R

library(shiny)
library(shinydashboard)

# Define UI for application that draws a histogram
ui<- dashboardPage(

dashboardHeader(title = "Test Application",titleWidth = "400px"),

dashboardSidebar(
 sidebarMenu(    #Add sidebarMenu here!!!!!
 menuItem("Page 1", tabName = "page1", icon = icon("dashboard")), # You can add icons to your menu if you want
 menuItem("Page 2", tabName = "page2", icon = icon("dashboard")))),

dashboardBody(
 tabItems(

    tabItem(
     tabName = "page1",
     h1("This is page 1")),

   tabItem( #Add checkboxGroupInput into this tabItem 
     tabName = "page2",
     h1("This is page 2"),
     checkboxGroupInput("outcome", "Select Outcome Variable(s):", choices = c("Box 1", "Box 2", "Box 3")),
     selectInput("selectinput", label = "Select:", choices = c("Choice 1", "Choice 2", "Choice 2")))
 )
))

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

}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Shinydashboard侧边栏上的输入未绘制的情节 - Plot not rendering with inputs on the sidebar of shinydashboard 如何在导航栏中添加按钮以在闪亮的仪表板中显示/隐藏侧边栏 - How to add a button in navbar to show/hide a sidebar in shiny like in shinydashboard R Shiny:如何使用Shinydashboard库在Shiny应用程序中显示数据框 - R Shiny: how to display dataframe in shiny app using shinydashboard library Shiny + CSS:在shinydashboard侧边栏中对齐actionButtons - Shiny + CSS: Aligning actionButtons in shinydashboard sidebar 锁定R闪亮仪表板侧边栏(shinydashboard) - Locking R shiny dashboard sidebar (shinydashboard) 以编程方式在闪亮仪表板中切换侧边栏菜单的显示 - Toggle display of sidebar menu in shinydashboard programmatically Shiny 选项卡被宽边栏遮挡 - Shiny tabs are obscured by a wide sidebar R Shiny 应用程序的 ShinyDashboard 侧边栏中的 selectInput 反应值仍然为 Null - selectInput reactive value in shinyDashboard sidebar of R shiny app remains Null 使用 Shiny 时自动更新 DT 有效,但在具有多个选项卡的闪亮仪表板中无效 - Automatically Updating a DT works when using Shiny, but not in shinydashboard with multiple tabs 删除在特定 tabPanel 的闪亮仪表板中显示或隐藏左侧边栏的图标 - Remove icon which displays or hides the left sidebar in shinydashboard for a certain tabPanel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM