0
votes

I am a beginner in R and working on a small project. Is there a way to apply two filters to a data frame in shiny? I am working on a dataframe that has multiple columns such as medical specialist, city etc. There are different types of specialists in the specialist column (Ortho, Periodontist and so on) I am try to get an output on shiny dashboard such that I filter the data by city and specialist at the same time. or two filters on the dashboard such that i filter first by city and then apply the specialist filter. The output will give me a list of all specialists (either ortho, perio or something else) in that city.

Here's the sample code i am working on. Thanks in advance.

output$origin <- renderUI({
    options <- sort(unique(city.df$specialist))
    selectInput("city","Select Specialist",as.list(options))
3
Have you actually tested the code? Please share the result. stackoverflow.com/help/how-to-ask. - T-Heron

3 Answers

1
votes

I'm not entirely sure what you want to achieve.

To slice a data.frame depending on two or more selected inputs is rather trivial.

On the other hand in your code snippet you refer to the shiny function renderUI, which is used of server-side functions. This is one of the most interesting aspects of shiny, but together with reactivity one of the most difficult to grasp for the novice.

This is a simple example (deliberately kept simple in formatting etc.) without renderUI:

library(shiny)

city <- c('London','Tokio','New York')
specialist <- c('ortho', 'perio','gyne', 'rhino')
df <- expand.grid(city = city, specialist = specialist)

opt_specialist <- sort(unique(df$specialist))
opt_city <- sort(unique(df$city))

ui <- fluidPage(
  selectInput("spec","Select Specialist",opt_specialist),
  selectInput("city","Select City",opt_city),
  verbatimTextOutput('city_'),
  verbatimTextOutput('spec_')

)

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

  output$city_ <- renderText({input$city})
  output$spec_ <- renderText({input$spec})

}

shinyApp(ui = ui, server = server)  

The example below uses server-side programming. Per se wouldn't make much sense to use it, unless in conjunction with more complex server-side operations.

library(shiny)

city <- c('London','Tokio','New York')
specialist <- c('ortho', 'perio','gyne', 'rhino')
df <- expand.grid(city = city, specialist = specialist)
opt_specialist <- sort(unique(df$specialist))
opt_city <- sort(unique(df$city))

ui <- fluidPage(
  uiOutput("origin"),
  verbatimTextOutput('city_'),
  verbatimTextOutput('spec_')

)

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

  output$origin <- renderUI({

    list(
    selectInput("spec","Select Specialist",opt_specialist),
    selectInput("city","Select City",opt_city)
    )
  })

  output$city_ <- renderText({input$city})
  output$spec_ <- renderText({input$spec})

}

shinyApp(ui = ui, server = server)

If I missed anything, please edit or extend your post and I'll amend the response accordingly.

1
votes

There are two options which You can do, the easiest:

using DT package and filtering it in the table:

iris2 = iris[c(1:10, 51:60, 101:110), ]
datatable(iris2, filter = 'top', options = list(
  pageLength = 5, autoWidth = TRUE
))

Thats the example from DT website: https://rstudio.github.io/DT/

Or you would have to create two selectInput widgets with choices of Your table and then filter Your dataset in server.R:

data_filt <- reactive({
        if (input$select1 != "..."){
        data <- data[data$column1 %in% input$select1,]
        }
        if (input$select2 != "..."){
        data <- data[data$column2 %in% input$column2,]
        }
data})
0
votes

Presumably in an ideal world you need the UI to be able to permit selection by either of the two variables first, with the dropdown for the subsequent variable(s) then limited to the universe of values returned by the first subsetting?