问题
Is it possible to create a checkbox widget within navbar line? This is an example of what I have in mind.
The following creates a checkboxInput, but the interactivity does not appear to work correctly:
library(shiny)
ui <- navbarPage(
"App Title",
tabPanel("Plot"),
navbarMenu("More",
tabPanel("Summary"),
tabPanel("Table")
),
tabPanel(checkboxInput("chk_1", "This is a label")),
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
)
server <- function(input, output, session) {
output$value <- renderPrint({ input$chk_1 })
}
shinyApp(ui, server)
回答1:
I'm surprised this works at all, as it uses the checkbox as the title of a tabPanel, and includes the hr() and output as elements in the navbarPage, not in a panel. Surprisingly, you can fix the specific updating problem simply by adding a reactive observer that updates the checkbox value. In your server function, you just need to add:
observe({
updateCheckboxInput(session, "chk_1", value=input$chk_1)
})
I would also add a value= "chk_1_panel" or some such to the panel represented by the checkbox, so it has a rational name when selected.
Note that when you select the checkbox, it will switch to a different panel. I slightly changed your code to move the output into a panel to emphasize this panel switch. The complete example is then:
library(shiny)
ui <- navbarPage(
"App Title",
tabPanel("Plot"),
navbarMenu( "More",
tabPanel("Summary"),
tabPanel("Table")
),
tabPanel( value= "chk_1_panel",
checkboxInput("chk_1", "This is a label"),
hr(),
fluidRow(column(3, verbatimTextOutput("value")))
)
)
server <- function(input, output, session) {
observe({
updateCheckboxInput(session, "chk_1", value=input$chk_1)
})
output$value <- renderPrint({ input$chk_1 })
}
shinyApp(ui, server)
来源:https://stackoverflow.com/questions/36360146/checkbox-widget-within-navbar-shiny