Search code examples
rshinyshinydashboard

Add string into a list and remove the text in the search bar of a SearchInput / textinput


Lets say I have a searchInput from shinyWidgets or regular textinput from Shiny on my app.

I want the user to able to write something, press enter - the code adds that into a list. Such that search_1 ENTER, search_2 ENTER, search_3 ENTER

That searchinput UI shall be reset everytime enter is hit.

Output needs to be "search_1, search_2, search_3"

I have workaround for now, with searchInput splits the input text with ", " so the user needs to type "search_1, search_2, search_3" themselves, which is not optimal for the use.

Hope could this be done? Thanks in advance!


Solution

  • You can create a reactive value search_text storing all inputs:

    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
      searchInput("search"),
      textOutput("text")
    )
    
    server <- function(input, output, session) {
      # store all entered search terms
      search_text <- reactiveVal(value = "")
      
      observeEvent(
        eventExpr = input$search,
        handlerExpr = {
          # add current search term
          search_text() %>%
            paste0(input$search, ", ") %>%
            search_text()
          
          # reset input text
          updateSearchInput(session, "search", value = "")
        }
      )
      
      output$text <- renderText(search_text())
    }
    
    shinyApp(ui, server)
    

    enter image description here