Kasia Kulma
Kasia Kulma

Reputation: 1722

shinydashboard: filtering DT by textInput

I want to build a pretty straightforward functionality where a user chooses a file to process and then different outputs based on the full content of the file are shown in various data.tables across multiple menu tabs. Additionally, I want a user to be able to filter all those data.tables by value present in all the output that would be taken from textInput field. So, it should follow the logic: if the textInput is empty, then show all the records otherwise show only those records that match the value from textInput. Easy, right?

Here's a dummy example:

### ui.R

library(shiny)
library(shinydashboard)
library(DT)

dbSidebar <- dashboardSidebar(
  sidebarMenu(
textInput(inputId = "search_term",
          label = "Search"),

# one of many tabs
menuItem("General Info", tabName = "general_info", icon = icon("info-sign", lib = "glyphicon")),

downloadButton('downloadData', 'Download',
               icon("paper-plane"), 
               style="color: #fff; background-color: #337ab7; border-color: #2e6da4;
               margin-top: 20px; margin-left: 15px;")
    )
  )

general_info_tab <- tabItem(tabName = "general_info",
                           fluidRow(
                             box(h2("Companies House Search Data"),
                                 DT::dataTableOutput('searchTable')
                                 ),
                             width = 12)
                           )

dashboardPage(
  dashboardHeader(),
  dbSidebar,
  dashboardBody(
tabItems(
  general_info_tab
    )
  )
)


### server.R

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)

# my hypothetical file content
dummy_data <- data.frame(
  a = rep(c(1,2,3), 6),
  b = rep(c("A2746", "38504", "CD759")),
  fruit = rep(c("apple", "pear"), each = 9)
)



shinyServer(function(input, output, session) {

  output$searchTable = DT::renderDataTable({

    selected <- dummy_data

if(!is.null(input$search_term) ||
   !is.na(input$search_term) ||
   length(input$search_term) > 0 ||
   input$search_term != "") {

  selected <- filter(dummy_data, b == input$search_term)

}

DT::datatable(selected)

  })
})

With this code I see empty dataset unless I have textInput values. How can I make it appear as a full dataset by default (when textInput field empty) and filter it down when I write text? Thanks

Upvotes: 2

Views: 610

Answers (1)

SeGa
SeGa

Reputation: 9809

The column b is stored as factors i think and the filter method is not working correctly.

The following example transforms the column b to character and it uses partial matching for the b column.

I used the tidyselect package for the partial filtering, although you could also do it with dplyr only. Then you should exchange the filter with this one: filter(dummy_data, grepl(input$search_term, b)).

library(tidyselect)

dummy_data$b <- as.character(dummy_data$b)

server <- shinyServer(function(input, output, session) {

  output$searchTable = DT::renderDataTable({
    selected <- dummy_data
    if (input$search_term != "") {
      selected <- filter(dummy_data, b == vars_select(dummy_data$b, contains(input$search_term)))
    }
    DT::datatable(selected)
  })

})

Upvotes: 2

Related Questions