how to filter data with shinywidget

How to filter the data on the basis of shinywidget drop down list.

library(shiny)
library(plotly)
library(dplyr)
library(readr)
library(shinydashboard)
library(shinythemes)
library(dashboardthemes)
library(shinyWidgets)
total<-tibble::tribble(
  ~Order.ID,  ~Order_Date,              ~State,                ~City, ~CustomerName, ~sales,
  "B-25601",   "1/4/2018",           "Gujarat",          "Ahmedabad",      "Bharat",  1275L,
  "B-25602",   "1/4/2018",       "Maharashtra",               "Pune",       "Pearl",    66L,
  "B-25603",   "3/4/2018",    "Madhya Pradesh",             "Bhopal",       "Jahan",     8L,
  "B-25604",   "3/4/2018",         "Rajasthan",             "Jaipur",      "Divsha",    80L,
  "B-25605",   "5/4/2018",       "West Bengal",            "Kolkata",     "Kasheen",   168L,
  "B-25606",   "6/4/2018",         "Karnataka",          "Bangalore",       "Hazel",   424L,
  "B-25607",   "6/4/2018", "Jammu and Kashmir",            "Kashmir",    "Sonakshi",  2617L,
  "B-25608",   "8/4/2018",        "Tamil Nadu",            "Chennai",     "Aarushi",   561L,
  "B-25609",   "9/4/2018",     "Uttar Pradesh",            "Lucknow",      "Jitesh",   119L,
  "B-25610",   "9/4/2018",             "Bihar",              "Patna",      "Yogesh",  1355L,
  "B-25611",  "11/4/2018",            "Kerala", "Thiruvananthapuram",       "Anita",    24L,
  "B-25612",  "12/4/2018",            "Punjab",         "Chandigarh",   "Shrichand",   193L,
  "B-25613",  "12/4/2018",           "Haryana",         "Chandigarh",      "Mukesh",   180L,
  "B-25614", "13-04-2018",  "Himachal Pradesh",              "Simla",     "Vandana",   116L,
  "B-25615", "15-04-2018",            "Sikkim",            "Gangtok",      "Bhavna",   107L,
  "B-25616", "15-04-2018",               "Goa",                "Goa",       "Kanak",    12L,
  "B-25617", "17-04-2018",          "Nagaland",             "Kohima",       "Sagar",    38L,
  "B-25618", "18-04-2018",    "Andhra Pradesh",          "Hyderabad",       "Manju",    65L,
  "B-25619", "18-04-2018",           "Gujarat",          "Ahmedabad",      "Ramesh",   157L,
  "B-25620", "20-04-2018",       "Maharashtra",               "Pune",      "Sarita",    75L,
  "B-25621", "20-04-2018",    "Madhya Pradesh",             "Bhopal",      "Deepak",    87L,
  "B-25622", "22-04-2018",         "Rajasthan",             "Jaipur",     "Monisha",    50L,
  "B-25623", "22-04-2018",       "West Bengal",            "Kolkata",      "Atharv",  1364L,
  "B-25624", "22-04-2018",         "Karnataka",          "Bangalore",        "Vini",   476L,
  "B-25625", "23-04-2018", "Jammu and Kashmir",            "Kashmir",       "Pinky",   257L,
  "B-25626", "23-04-2018",       "Maharashtra",             "Mumbai",      "Bhishm",   856L,
  "B-25627", "23-04-2018",    "Madhya Pradesh",             "Indore",      "Hitika",   485L,
  "B-25628", "24-04-2018",             "Bihar",              "Patna",       "Pooja",    25L,
  "B-25629", "24-04-2018",            "Kerala", "Thiruvananthapuram",      "Hemant",  1076L,
  "B-25630", "24-04-2018",            "Punjab",         "Chandigarh",       "Sahil",   107L,
  "B-25631", "24-04-2018",           "Haryana",         "Chandigarh",        "Ritu",    68L,
  "B-25632", "25-04-2018",  "Himachal Pradesh",              "Simla",      "Manish",   781L,
  "B-25633", "26-04-2018",            "Sikkim",            "Gangtok",        "Amit",    43L,
  "B-25634", "26-04-2018",               "Goa",                "Goa",      "Sanjay",    30L,
  "B-25635", "26-04-2018",          "Nagaland",             "Kohima",       "Nidhi",   160L,
  "B-25636", "26-04-2018",       "Maharashtra",             "Mumbai",       "Nishi",   259L,
  "B-25637", "26-04-2018",    "Madhya Pradesh",             "Indore",       "Ashmi",  1603L,
  "B-25638", "26-04-2018",       "Maharashtra",               "Pune",       "Parth",   494L,
  "B-25639", "27-04-2018",    "Madhya Pradesh",             "Bhopal",       "Lisha",    98L,
  "B-25640", "27-04-2018",         "Rajasthan",             "Jaipur",     "Paridhi",    68L,
  "B-25641", "27-04-2018",       "West Bengal",            "Kolkata",     "Parishi",    42L,
  "B-25642", "28-04-2018",         "Karnataka",          "Bangalore",        "Ajay",   116L,
  "B-25643", "29-04-2018", "Jammu and Kashmir",            "Kashmir",       "Kirti",    22L,
  "B-25644", "30-04-2018",       "Maharashtra",             "Mumbai",      "Mayank",    14L,
  "B-25645",   "1/5/2018",    "Madhya Pradesh",             "Indore",      "Yaanvi",   305L,
  "B-25646",   "1/5/2018",             "Bihar",              "Patna",       "Sonal",   362L,
  "B-25647",   "3/5/2018",            "Kerala", "Thiruvananthapuram",      "Sharda",    12L,
  "B-25648",   "4/5/2018",            "Punjab",         "Chandigarh",      "Aditya",   353L,
  "B-25649",   "5/5/2018",           "Haryana",         "Chandigarh",      "Rachna",   193L,
  "B-25650",   "6/5/2018",       "Maharashtra",             "Mumbai",      "Chirag",   233L,
  "B-25651",   "7/5/2018",    "Madhya Pradesh",             "Indore",      "Anurag",   228L,
  "B-25652",   "8/5/2018",               "Goa",                "Goa",     "Tushina",   333L,
  "B-25653",   "8/5/2018",          "Nagaland",             "Kohima",       "Farah",   534L,
  "B-25654",  "10/5/2018",       "Maharashtra",             "Mumbai",       "Sabah",    53L,
  "B-25655",  "11/5/2018",    "Madhya Pradesh",             "Indore",        "Nida",   158L,
  "B-25656",  "11/5/2018",       "Maharashtra",               "Pune",    "Priyanka",   149L,
  "B-25657", "13-05-2018",    "Madhya Pradesh",             "Bhopal",      "Tulika",   105L,
  "B-25658", "14-05-2018",         "Rajasthan",             "Jaipur",     "Shefali",    26L,
  "B-25659", "15-05-2018",       "West Bengal",            "Kolkata",   "Sanskriti",    97L,
  "B-25660", "16-05-2018",         "Karnataka",          "Bangalore",      "Shruti",    59L,
  "B-25661", "17-05-2018", "Jammu and Kashmir",            "Kashmir",  "Subhashree",   635L,
  "B-25662", "17-05-2018",       "Maharashtra",             "Mumbai",       "Sweta",    46L,
  "B-25663", "19-05-2018",    "Madhya Pradesh",             "Indore",  "Pournamasi",  1103L,
  "B-25664", "20-05-2018",             "Bihar",              "Patna", "Pratyusmita",    55L,
  "B-25665", "21-05-2018",            "Kerala", "Thiruvananthapuram",   "Chayanika",    45L)


ui <- dashboardPage(
  dashboardHeader(title="dashboard"),
  dashboardSidebar(
    sidebarMenu(
      pickerInput(
        inputId = "p1",
        label = "Static",
        multiple = TRUE,
        choices = unique(c(total$State)),
        options = list(`selected-text-format`= "static",
                       title = "select a state")
      ),
      pickerInput(
        inputId = "p2",
        label = "Static",
        multiple = TRUE,
        choices = unique(c(total$City)),
        options = list(`selected-text-format`= "static",
                       title = "select a city")
      ))),
  dashboardBody(
    shinyDashboardThemes(
      theme = "onenote"
    ),
    fluidPage(
      plotlyOutput("state", height = 200),
      plotlyOutput("city", height = 200),
      plotlyOutput("customer", height = 200),
      plotlyOutput("sales", height = 300),
      dataTableOutput("datatable"))))

axis_titles <- . %>%
  layout(
    xaxis = list(title = ""),
    yaxis = list(title = "Sales"))

server <- function(input, output, session) {
  
  State <- reactiveVal()
  City <- reactiveVal()
  CustomerName<- reactiveVal()
  Order_Date <- reactiveVal()
  
  observeEvent(event_data("plotly_click", source = "State"), {
    State(event_data("plotly_click", source = "State")$x)
    City(NULL)
    Order_Date(NULL)
  })
  
  observeEvent(event_data("plotly_click", source = "City"), {
    City(event_data("plotly_click", source = "City")$x)
    CustomerName(NULL)
    Order_Date(NULL)
  })
  
  observeEvent(event_data("plotly_click", source = "CustomerName"), {
    CustomerName(event_data("plotly_click", source = "CustomerName")$x)
    Order_Date(NULL)
  })
  
  observeEvent(event_data("plotly_click", source = "Order_Date"), {
    Order_Date(event_data("plotly_click", source = "Order_Date")$x)
  })
  
  output$state <- renderPlotly({
    total%>%
      count(State, wt = sales) %>%
      plot_ly(x = input$p1, y = ~n, source = "State") %>%
      axis_titles() %>% 
      layout(title = "State")
  })
  
  output$city <- renderPlotly({
    if (is.null(State())) return(NULL)
    total%>%
      filter( State %in% State()) %>%
      count(City, wt = sales) %>%
      plot_ly(x = ~City, y = ~n, source = "City") %>%
      axis_titles() %>%
      layout(title = State())
  })
  
  
  output$customer <- renderPlotly({
    if (is.null(City())) return(NULL)
    total%>%
      filter(City %in% City()) %>%
      count(CustomerName, wt = sales) %>%
      plot_ly(x = ~CustomerName, y = ~n, source = "CustomerName") %>%
      axis_titles() %>%
      layout(title = City())
  })
  
  output$sales <- renderPlotly({
    if (is.null(CustomerName())) return(NULL)
    
    total %>%
      filter(CustomerName %in% CustomerName()) %>%
      count(Order_Date, wt = sales) %>%
      plot_ly(x = ~Order_Date, y = ~n, source = "Order_Date") %>%
      add_lines() %>%
      axis_titles() %>%
      layout(title = paste(CustomerName(), "sales over time"))
  })
  
  output$datatable <- renderDataTable({
    if (is.null(Order_Date())) return(NULL)
    
    total %>%
      filter(
        CustomerName %in% CustomerName(),
        as.Date(Order_Date) %in% as.Date(Order_Date())
      )
  })
  
}
runApp(list(ui = ui, server = server), launch.browser = TRUE)

sorry for not providing the proper reprex for this since I am facing some kind of issue for rendering reprex,I will try to fix it in the future for better code explanation.

This is too much code to ask an answer a seemingly simple question...

shiny provides a snippet to quickly make a minimial shiny app.
in R studio if you type shinyapp into a blank script, and then tab a minimal frame for shiny is provided, recommended you then populate that with the fewest things to show and ask your questions.

Thanks

This topic was automatically closed 21 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.