如何删除radioButtons上的textInput单击R闪亮

时间:2018-02-12 23:01:40

标签: r shiny

我面临的任务是创建一个反应性文本输入表单。根据单选按钮选择的值,将显示一个或两个textInput字段。到现在为止还挺好。但是当从2个文本输入更改为1时,将预先设置第二个输入的值。我尝试使用#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) } 删除textInput值,但它没有按预期工作。这是我使用的代码:

create-react-app

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

  1. 现在点击单选按钮值1后只获得一个textInput,“Hello”消失但“世界”仍然存在(我通常认为这是一件好事,但现在我宁愿删除它): enter image description here

1 个答案:

答案 0 :(得分:2)

这是使用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)
}