【问题标题】:Disable a selectInput in R Shiny when not required不需要时在 R Shiny 中禁用 selectInput
【发布时间】:2017-07-12 23:22:01
【问题描述】:

我正在构建一个 shiny 网页,其表单由两个 selectInput 组成:第一个 - 静态 - 在 ui 部分,第二个 - 动态 - 在 server 部分。实际问题的简化如下所示。

require(shiny)

ui <- fluidPage(

    # The static input
    selectInput(inputId = 'static',
                label = 'Make a choice',
                choices = c('A', 'B', 'C'),
                selectize = FALSE),

    # The dynamic input
    uiOutput(outputId = 'dynamicInput'),

    # The output
    hr(),
    strong('This is a check for the output:'),
    textOutput(outputId = 'check')

)

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

    # The dynamic input definition
    output$dynamicInput <- renderUI({

        # This input exists if the `static`
        # one is equal to `A` only
        if (input$static == 'A') {
            selectInput(inputId = 'dynamic',
                        label = 'Choose a subset for `A`',
                        choices = c('A1', 'A2', 'A3'),
                        selectize = FALSE)
        } else {
            return(NULL)
        }

   })

   # The example output
   output$check <- renderText({

       paste(input$static, input$dynamic)

   })

}

shinyApp(ui, server)

在实际情况下,我必须根据静态输入以及最终的动态输入(如果存在)在数据库上启动查询。

在测试应用程序时,我在第一轮正确获得了动态 selectInput,并且我能够选择 - 比如说 - A2 选项。然后,如果我在静态selectInput中选择B,动态输入没有响应,仍然显示之前的选择而不是显示NULL

如果静态不等于A,如何强制重置动态selectInput?我是否必须使用诸如Dean Attalishinyjs 之类的技巧手动隐藏它?

【问题讨论】:

  • 您需要隐藏 UI(在您给出的示例中已经发生)还是需要“重置”input$dynamic 的值? shinyjs 只会帮助你处理前者,但这已经发生了,至少在你给出的简化示例中
  • 两者,但考虑到动态输入在不请求时消失,第一个已经可以了。我还希望动态selectInput 在静态不等于A 时返回NULL

标签: r shiny


【解决方案1】:

试试这个。它是使用响应值和观察值的组合。

require(shiny)

ui <- fluidPage(

  # The static input
  selectInput(inputId = 'static',
              label = 'Make a choice',
              choices = c('A', 'B', 'C'),
              selectize = FALSE),

  # The dynamic input
  uiOutput(outputId = 'dynamicInput'),

  # The output
  hr(),
  strong('This is a check for the output:'),
  textOutput(outputId = 'check')

)

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

  ## list to store reactive values
  values <- reactiveValues()


  # The dynamic input definition
  output$dynamicInput <- renderUI({

    # This input exists if the `static`
    # one is equal to `A` only
    if (input$static == 'A') {
      selectInput(inputId = 'dynamic',
                  label = 'Choose a subset for `A`',
                  choices = c('A1', 'A2', 'A3'),
                  selectize = FALSE)
    } else {
      return(NULL)
    }

  })

  ## this bit fixes the issue
  observe({
    if (input$static == "A") {
      values$dyn <- input$dynamic
    } else {
      values$dyn <- NULL
    }
  })

  # The example output
  output$check <- renderText({

    paste(input$static, values$dyn)

  })

}

shinyApp(ui, server)

【讨论】:

  • 拍得好!所以基本上诀窍是在动态输入和输出之间添加一个过滤器。有点复杂,但它有效。谢谢!
猜你喜欢
  • 2020-07-08
  • 2021-08-04
  • 2017-02-18
  • 2017-07-07
  • 2018-03-12
  • 2021-01-25
  • 2018-06-22
  • 2021-12-14
  • 2020-01-23
相关资源
最近更新 更多