how to reset input$search when clicking fileInput button

I use the fileInput Browse button as a tool to refresh the shinyapp including the variable input$search. The output$table <- DT::renderDataTable({}) displays the table that is loaded from fileInput Browse, and then subtable is displayed by inputting key word into the inside search box. However, when the second file is loaded, the value of variable input$search is kept. I want to change the input$search to NULL or empty string or initial status. Below is my codes.

shinyUI(fluidPage(
    shinyjs::useShinyjs(),
    theme = shinytheme("spacelab"),
    sidebarLayout(
        sidebarPanel(
             p(strong('Inputs')),
             hr(),
             fileInput("file1", "Upload clonotype input file", accept = ".csv")
        ),
        mainPanel(
            tabsetPanel(
                  id = "tabs",
                  tabPanel(title = "table",tags$br(),DT::dataTableOutput("table")),
            )
        )
    )
))

shinyServer(function(input, output) {
    datasetInput <- reactive({
        file <- input$file1
        ext <- tools::file_ext(file$datapath)
        req(file)
        validate(need(ext == "csv", "Please upload a csv file"))
        read.csv(file$datapath, header = T, sep=',',stringsAsFactors = F)
    })
    output$table <- DT::renderDataTable({
        mytbl = datasetInput()
        DT::datatable(
            mytbl, 
            callback = JS(
                 "table.on( 'search.dt', function () {",
                 "Shiny.setInputValue( 'search', table.search() );",
                 "} );"
            ),
            rownames=FALSE,
       )
    })
})
# Run the application 
shinyApp(ui = shinyUI, server = shinyServer)

You could reset input$search on file upload:

library(DT)
library(shiny)
library(shinyjs)
library(shinythemes)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  theme = shinytheme("spacelab"),
  sidebarLayout(sidebarPanel(
    p(strong('Inputs')),
    hr(),
    fileInput("file1", "Upload clonotype input file", accept = ".csv")
  ),
  mainPanel(tabsetPanel(
    id = "tabs",
    tabPanel(title = "table", tags$br(), DTOutput("table")),
  )))
)

server <- function(input, output, session) {
  datasetInput <- reactive({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    read.csv(
      file$datapath,
      header = TRUE,
      sep = ',',
      stringsAsFactors = FALSE
    )
  })
  
  output$table <- renderDT({
    mytbl = datasetInput()
    datatable(
      mytbl,
      callback = JS(
        "table.on( 'search.dt', function () {",
        "Shiny.setInputValue( 'search', table.search() );",
        "} );"
      ),
      rownames = FALSE,
    )
  })
  
  observeEvent(input$file1, {
    runjs("Shiny.setInputValue( 'search', null );")
  })
  
  observe({
    print(input$search)
  })
}

shinyApp(ui, server)

It is working. nice. Thank you ismirsehregal!

The function output$table <- renderDataTable({}) can output filtered table based on value of input$search,. However, the search result is based on both lower(value) of input$search., plus upper(value) of input$search.

Additional question is that how to convert string value of input$search to uppercase in search box, if user inputs lowercase characters from search box, or how to change search rule to case sensitive.

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.