【问题标题】:How to delete a textInput upon a radioButtons click in R shiny如何在 R Shiny 中单击单选按钮时删除 textInput
【发布时间】:2018-02-12 23:01:40
【问题描述】:

我面临的任务是创建一个反应式文本输入表单。根据单选按钮选择的值,应显示一个或两个 textInput 字段。到目前为止,一切都很好。但是当从 2 个文本输入更改为 1 个时,第二个输入的值会被保留。我尝试使用 observe() 删除 textInput 值,但它没有按预期工作。这是我使用的代码:

#library(shiny)
if (interactive()) {

  ui <- fluidPage(
    radioButtons("controller", "No. inputs", choices = c(1, 2), selected = 2),
    uiOutput("text_fields"),
    verbatimTextOutput("test")
  )

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

    output$text_fields <- renderUI({  
      if(input$controller == 2){
        fluidRow(
          column(6, textInput("inText_1", label = "Input text 1", value = "")),
          column(6, textInput("inText_2", label = "Input text 2", value = ""))
        )
      }else{
        fluidRow(textInput("inText_1", label = "Input text 1", value = ""))
      }
    })

    output$test <- renderText(c(input$inText_1, input$inText_2))

    observe({
      # The input$controller variable needs to be used inside the observe, correct?
      x <- input$controller

      # This will delete the values of text input, based on change of x
      updateTextInput(session, "inText_1", value = "")
      updateTextInput(session, "inText_2", value = "")
    })
  }

  shinyApp(ui, server)
}

以下两张图显示了我的问题: 1. 从两个输入“Hello”和“World”开始

  1. 现在单击单选按钮值 1 以仅获取一个 textInput 后​​,“Hello”消失了,但“World”仍然存在(我通常会说这是一件好事,但现在我宁愿删除它):

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是使用conditionalPanel 的好案例:

    library(shiny)
    if (interactive()) {
    
        ui <- fluidPage(
            radioButtons("controller", "No. inputs", choices = c(1, 2), selected = 2),
            fluidRow(
                column(6, textInput("inText_1", "Input text 1", value = "")),
                column(6, conditionalPanel('input.controller == 2',
                   textInput("inText_2", "Input text 2", value = "")))
            ),
            verbatimTextOutput("test")
        )
    
        server <- function(input, output, session) {
    
            output$test <- renderText(c(input$inText_1, input$inText_2))
    
            observe({
                # The input$controller variable needs to be used inside the observe, correct?
                x <- input$controller
    
                # This will delete the values of text input, based on change of x
                updateTextInput(session, "inText_1", value = "")
                updateTextInput(session, "inText_2", value = "")
            })
    
        }
    
        shinyApp(ui, server)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-01
      • 1970-01-01
      相关资源
      最近更新 更多