简体   繁体   中英

how do you clear the value in textInput box in shiny

I have a actionButton called addbtn. When clicked this button, it will take the input from a textInput box and create additional textInput's.

When I click the addbtn, and the textInput boxes create, the values in the txtInput box needs to clear so that I can add more textInput boxes.

This is the code:

ui:

ibrary(shiny)

  shinyUI(

    # Use a fluid Bootstrap layout
    fluidPage(    


      # Generate a row with a sidebar
      sidebarLayout(      


        # Define the sidebar with one input
        sidebarPanel(
          sliderInput("capacity", "Current Capacity:", 
                      min=0, max=100, value=10),
          c(list(
            textInput("service", "Application Component Name", ""),
            actionButton("addbtn", "Add Component"))),
            #lapply(seq(10), function(i) uiOutput(paste0("ui", i)))

            br(),
            br(), 
            br(),
            br(),
            br(),
          actionButton("calcbtn", "Calculate Projection")




        ),



        # Create a spot for the barplot
        mainPanel(
          textInput("inputWork","Volume", width="200px"),
          textInput("inputGrowth","Growth Rate", width="100px"),
          lapply(seq(10), function(i) uiOutput(paste0("ui", i)))
          #tags$p("Web"),
          #verbatimTextOutput("input_type_text")

        )

      )
    )
  )

server <- function(input, output) 
{
  observeEvent(input$addbtn, {
    n <- isolate(input$addbtn)
    if (n == 0) return()

    # create n-th pair of text input and output
    output[[paste0("ui", n)]] <- renderUI(
      list(textInput(paste0("textin", n), isolate(input$service)),
           textOutput(paste0("textout", n))))
    updateTextInput(input$service, "Application Component Name", value="")
  })
  }

with this code, I am getting these errors:

Warning: Error in $: $ operator is invalid for atomic vectors
Stack trace (innermost first):
    64: updateTextInput
    63: observeEventHandler [C:\shiny\bcl/server.R#11]
     1: shiny::runApp
ERROR: [on_request_read] connection reset by peer

All your problem in updateTextInput

in help

Arguments

session The session object passed to function given to shinyServer.

inputId The id of the input object.

label The label to set for the input object.

value The value to set for the input object.

so your need server like :

shinyServer(function(input, output,session) {

  observeEvent(input$addbtn, {
    n <- isolate(input$addbtn)
    if (n == 0) return()

    # create n-th pair of text input and output
    output[[paste0("ui", n)]] <- renderUI(
      list(textInput(paste0("textin", n), isolate(input$service)),
           textOutput(paste0("textout", n))))
    updateTextInput(session,"service", "Application Component Name", value="")
  })
  })

If your dont want to change label of textInput you can drop it in update : updateTextInput(session,"service", value="")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM