首页 > 解决方案 > 如何以编程方式将 R-Shiny “checkboxinput” 值更改为 FALSE/TRUE?

问题描述

我想在运行时将复选框输入值更改为 FALSE/TRUE。我怎样才能做到这一点?

checkboxInput(inputId = "smoother", label = "Overlay smooth trend line", value = FALSE)

标签: rcheckboxshiny

解决方案


您可以使用updateCheckboxInput(). 请参见下面的示例:

可重现的例子:

library(shiny)
ui <- fluidPage(
  actionButton(
    inputId = "check",
    label = "update checkbox"
  ),
  checkboxInput(
    inputId =  "checkbox", 
    label = "Input checkbox"
  )
)

server <- function(input, output, session) {
  observeEvent(
    eventExpr = input$check, {
      updatedValue = !input$checkbox

      updateCheckboxInput(
        session =  session,
        inputId =  "checkbox", 
        value = updatedValue
      )
    }
  )
}

shinyApp(ui, server)

推荐阅读