导航栏中的复选框小部件闪亮 [英] checkbox widget within navbar shiny
问题描述
是否可以在导航栏行中创建复选框小部件?这是我所想到的示例.
Is it possible to create a checkbox widget within navbar line? This is an example of what I have in mind.
以下内容创建了checkboxInput
,但交互性似乎无法正常工作:
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)
推荐答案
我对此感到惊讶,因为它使用复选框作为tabPanel
的标题,并包含hr()
和输出作为元素在navbarPage
中,而不是在面板中.出人意料的是,您可以简单地通过添加可更新复选框值的反应式观察器来解决特定的更新问题.在server
函数中,您只需添加:
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)
})
我还将在复选框所代表的面板中添加一个value= "chk_1_panel"
或类似名称,因此在选择该名称时会使用一个合理的名称.
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)
这篇关于导航栏中的复选框小部件闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!