首页 > 解决方案 > 取消选中框,但可以重新选中 R Shiny

问题描述

在我正在开发的闪亮应用程序中,有一个复选框,当我单击操作按钮时,我想将其重置为关闭。我发现当我单击按钮时取消选中该框的代码,但随后该按钮永久保持未选中状态,我无法重新选中它。有谁知道如何在按下按钮后取消选中该框,同时保持选中该框的能力?

以下是我已经尝试过的一些遇到上述问题的尝试:

ui <- fluidPage(

   # Application title
   titlePanel("Preflop Trainer"),

   sidebarLayout(
      sidebarPanel(
         checkboxInput("checkbox",
                     "Check Box"),
        actionButton("reset",
                      "Reset the box")
      )
)

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

     #I tried this
     if (input$reset) {
       if (input$reset != number) {
         updateCheckboxInput(session,"checkbox","Check Box",value = F)
       }
       number <- input$reset
     }

     #And I've tried this
     observeEvent(input$reset, {
       updateCheckboxInput(session,"checkbox","Check Box",value = F)
     }

}

标签: rshiny

解决方案


请再试一次。在纠正了一些括号问题后,这对我有用:

library(shiny)

ui <- fluidPage(

  # Application title
  titlePanel("Preflop Trainer"),

  sidebarLayout(mainPanel = 
      mainPanel(),
      sidebarPanel = 
      sidebarPanel(
        checkboxInput("checkbox",
                      "Check Box"),
        actionButton("reset",
                     "Reset the box")
      )

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


  #this works for me
  observeEvent(input$reset, {
    updateCheckboxInput(session, "checkbox", "Check Box", value = F)
  }
  )
}

shinyApp(ui, server)

推荐阅读