Restoring previous text input value

I have a code that generates UI depending on the switch value. In order to preserve value typed in searchInput() field in the case of clicking on the switch twice (i.e. switching to the fileInput UI and back to searchInput UI), I assign a reactive value that updates on the click of search and reset buttons.

I have several places in the app where I want to have similar functionality, hence I'd like to make a function that would have single param - id - and execute both observeEvent to update rv value. My attempt is below. Unfortunately, it returns only a bench of errors (Error in eval_tidy: object 'input' not found; Error in as.vector: cannot coerce type 'environment' to vector of type 'character'). How should I change this function?

P.S. Demo app is written as a single file, but in my real case app, I use modules.
P.P.S. observeEvent(input$search_term_search ...) works only on the mouse click, while the widget responds to both mouse click and Enter hit. How should I refer to the event of Enter hit?

Demo app code

library(shiny)
library(shinyWidgets)


# Define UI for application that draws a histogram
ui <- shiny::fluidPage(
    
    
    materialSwitch(
        inputId = "batch",
        label = "Batch mode",
        value = FALSE, 
        status = "info"),
    
    br(),
    
    htmlOutput(outputId = "search_input")
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    
    rv <- reactiveValues()
   # Reactive value that control the "default" value for searchInput field.
    rv$search_term <- ""
   # It's updeted upon clicking on search button 
    observeEvent(input$search_term_search, {
        rv$search_term <- input$search_term
    })
   # Or reset button
    observeEvent(input$search_term_reset, {
        rv$search_term <- ""
    })
    
#Code to generate UI
    output$search_input <- renderUI({
        
        if(isTRUE(input$batch)){
            
            field <- shiny::fileInput(
                           inputId = "search_batch",
                           label = "Select  file"
                       )
            return(field)
        
        } else {
            
            field <- shinyWidgets::searchInput(
                inputId = "search_term", 
                label = "text",
                placeholder = "...",
                value = rv$search_term, #here I use reactive "default"
                btnSearch = icon("search"), 
                btnReset = icon("remove")
            )
            return(field)
        }
    })
    
    
}

# Run the application 
shinyApp(ui = ui, server = server)

Function code

update_search_value <- function(id){
  srch <- paste0(id, "_search")
  
  observeEvent(input[[srch]], {
    x <- input[[id]]
    return(x)
  })
  
  rst <- paste(id, "_reset")
  observeEvent(input[[rst]], {
    x <- ""
  })
}

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.