首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当selectInput()被checkboxInput()禁用时,始终从它中选择特定的项

当selectInput()被checkboxInput()禁用时,始终从它中选择特定的项
EN

Stack Overflow用户
提问于 2020-03-21 09:37:24
回答 1查看 217关注 0票数 1

假设我在Shiny应用程序下面-

代码语言:javascript
运行
复制
library(shiny)
library(shinyjs)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    checkboxInput("checkbox", label = "Choice a value", value = TRUE),
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear"))
  ),
  server = function(input, output) {
    observeEvent(input$checkbox, {
            toggleState("variable")
        })
  }
)

现在,如果单击了checkbox,那么总是从variable中选择Cylinders

按照上述方法,这是不可能发生的。当前当用户选择其他值i.g时。Gears在启用variable之后,然后用户继续使用checkbox禁用variableGears的选择仍然保留在UI中,我不想这样做,而且每当variable被禁用时,都想立即返回Cylinders

任何指针如何完成这一点将受到高度赞赏。

bretauv's 回复后的更新-

他的回答对shiny's本机selectInput()很有用,但是如果我使用shinyWidgets包的pickerInput(),如下所示,似乎不起作用-

代码语言:javascript
运行
复制
library(shiny)
library(shinyjs)
library(shinyWidgets)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    checkboxInput("checkbox", label = "Choice a value", value = FALSE),
    pickerInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear"),
                 width = "50%",
                selected = "gear")
  ),
  server = function(input, output, session) {
    observe({
            if (input$checkbox) {
                    toggleState(id = "variable")
                } else {
                    toggleState(id = "variable")
                    updatePickerInput(session = session,
                                        inputId = "variable", label = NULL,  
                                        choices = c("Cylinders" = "cyl",
                                                      "Transmission" = "am",
                                                      "Gears" = "gear"),
                                        selected = "Cylinders")
                }
            })
  }
)

任何用于解决此问题的指针都将受到高度赞赏。

EN

回答 1

Stack Overflow用户

发布于 2020-03-21 10:02:57

您可以使用updateSelectInput (不要忘记在function(input, output, session)中添加session )。另外,当checkboxInputTRUEFALSE时,您需要详细说明情况。

下列守则应能发挥作用:

代码语言:javascript
运行
复制
library(shiny)
library(shinyjs)

shinyApp(
  ui = fluidPage(
    useShinyjs(),
    checkboxInput("checkbox", label = "Choice a value", value = FALSE),
    selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear"),
                selected = "gear")
  ),
  server = function(input, output, session) {
    observe({
      if(input$checkbox == "TRUE"){
        toggleState(id = "variable")
      }

      else if(input$checkbox == "FALSE"){
        toggleState(id = "variable")
        updateSelectInput(session = session,
                          inputId = "variable",
                          choices = c("Cylinders" = "cyl",
                                      "Transmission" = "am",
                                      "Gears" = "gear"),
                          selected = "cyl")
      }

    })
  }
)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60786330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档