隐藏和清除 selectInput

Hide and clear selectInput

我需要 show\hide 输入,如果输入不存在,我会得到 NULL 或空字符串,这里是可重现的例子:

ui <- 
  dashboardPage(
    dashboardHeader(
      title = 'Test'),
    dashboardSidebar(),
    dashboardBody(
      selectInput(
        inputId = 'mainInput',
        label = 'Main input',
        selected = 'Show',
        choices = c('Show', 'Hide')
      ),
      uiOutput(
        outputId = 'secondInputUI'
      ),
      actionButton(
        inputId = 'thirdInput',
        label = 'Check value'
      )
    )
)
server <- function(input, output, session){
  observeEvent(input$mainInput, ignoreNULL = TRUE, {
    if (input$mainInput == 'Show')
      output$secondInputUI <- 
        renderUI(
          selectInput(
            inputId = 'secondInput',
            label = 'Second input',
            selected = 0,
            multiple = FALSE,
            choices = c(1, 0)
          )
        )
    else {
      output$secondInputUI <- 
        renderUI(
          selectInput(
            inputId = 'secondInput',
            label = 'Second input',
            selected = '',
            multiple = TRUE,
            choices = c(1, 0)
          )
        )
      # If uncommit - input value don't update and will return latest available before delete input
      # output$secondInputUI <- 
      #   NULL
    }
  })

  observeEvent(input$thirdInput, {
    showNotification(
      session = session, 
      ui = paste(input$secondInput, collapse = ', '))
  })
}

shinyApp(
  ui = ui,
  server = server)

您可以看到将 NULL 设置为 ui 输出的注释部分,如果它处于活动状态 - 在清除 ui 之前闪亮 return 最新可用值,那么如何处理呢?

我想我明白了。您可以创建一个独立于 UI 的反应变量,因为当删除 UI 元素时输入不会更新。

library(shiny)
library(shinydashboard)

ui <- 
  dashboardPage(
    dashboardHeader(
      title = 'Test'),
    dashboardSidebar(),
    dashboardBody(
      selectInput(
        inputId = 'mainInput',
        label = 'Main input',
        selected = 'Show',
        choices = c('Show', 'Hide')
      ),
      uiOutput(
        outputId = 'secondInputUI'
      ),
      actionButton(
        inputId = 'thirdInput',
        label = 'Check value'
      )
    )
  )
server <- function(input, output, session){

  secondInputVar <- reactive({
    if(input$mainInput == 'Show'){
      input$secondInput
    } else {

    }
  })

  observeEvent(input$mainInput, ignoreNULL = TRUE, {
    if (input$mainInput == 'Show')
      output$secondInputUI <- 
        renderUI(
          selectInput(
            inputId = 'secondInput',
            label = 'Second input',
            selected = 0,
            multiple = FALSE,
            choices = c(1, 0)
          )
        )
    else {
      output$secondInputUI <- renderUI({
        NULL
      })
    }
  })

  observeEvent(input$thirdInput, {
    showNotification(
      session = session, 
      ui = paste(secondInputVar(), collapse = ', '))
  })
}

shinyApp(
  ui = ui,
  server = server)

所以,我找到了另一个解决方案,主要思想是:更新第一个输入的观察者中的输入值,对第二个输入隐藏观察者的第二个输入。如果我展示会更好:

ui <- 
  dashboardPage(
    dashboardHeader(
      title = 'Test'),
    dashboardSidebar(),
    dashboardBody(
      selectInput(
        inputId = 'mainInput',
        label = 'Main input',
        selected = 'Show',
        choices = c('Show', 'Hide')
      ),
      uiOutput(
        outputId = 'secondInputUI'
      ),
      actionButton(
        inputId = 'thirdInput',
        label = 'Check value'
      )
    )
)
server <- function(input, output, session){
  observeEvent(input$mainInput, {
    if (input$mainInput == 'Show')
      output$secondInputUI <- 
        renderUI(
          selectInput(
            inputId = 'secondInput',
            label = 'Second input',
            selected = 0,
            multiple = FALSE,
            choices = c(1, 0)
          )
        )
    else {
      output$secondInputUI <- 
        renderUI(
          selectInput(
            inputId = 'secondInput',
            label = 'Second input',
            selected = '',
            multiple = TRUE,
            choices = c(1, 0)
          )
        )
    }
  })

  # THE TRICK HERE ####
  observeEvent(input$secondInput, ignoreNULL = FALSE, {
    if (input$mainInput != 'Show'){
      output$secondInputUI <-
        renderUI(NULL)
    }
  })

  observeEvent(input$thirdInput, {
    showNotification(
      session = session, 
      ui = paste(input$secondInput, collapse = ', '))
  })
}

shinyApp(
  ui = ui,
  server = server)