why multiple selection throwing error in drop down list while single selection is working fine

why single selection is working fine while multiple selection throwing error in shiny widget drop down reactive list. Could anyone plase tell me why it is happening and keeps throwing error for multiple selection.

library(shiny)
library(plotly)
library(dplyr)
library(xts)
library(lubridate)
library(shinycssloaders)
library(shinycustomloader)
library(htmltools)
library(htmlwidgets)
library(shinyWidgets)
# re<-read.csv("c:/users/dell/desktop/ssss.csv")
re<-tibble::tribble(
      ~Order.ID, ~Order_Date, ~CustomerName,             ~State,     ~City, ~sales,
      "B-26180",  "4/5/2017", "Bhaggyasree",   "Andhra Pradesh", "Udaipur",   880L,
      "B-26248",  "4/6/2017",       "Mohit",            "Bihar", "Udaipur",   121L,
      "B-26262",  "4/7/2017",    "Vaibhavi", "Himachal Pradesh", "Udaipur",    24L,
      "B-26280",  "4/8/2017",      "Akshay", "Himachal Pradesh", "Udaipur",   145L,
      "B-26162",  "4/8/2017",      "Mitali",        "Karnataka", "Udaipur",   133L,
      "B-26254",  "4/9/2017",     "Noshiba",        "Karnataka", "Udaipur",   312L,
      "B-26156",  "4/9/2017",   "Chayanika",   "Madhya Pradesh", "Udaipur",  1103L)

ui <- fluidPage(
  titlePanel("Plotly - dateRangeInput"),
  sidebarLayout(
    sidebarPanel(
      pickerInput(
    inputId = "state",
    label = "state",
    multiple = TRUE,
    selected = re$State[1],
    choices = c(unique(re$State))),
      dateRangeInput(inputId = "date",label = "Date",
                 start = Sys.Date() - 28, end = Sys.Date() + 2,
                 format = "mm/dd/yyyy",
                 separator = "TO")),
    mainPanel(
      fluidPage(withLoader(plotlyOutput("p1"),type = "html",loader = "loader2")))))

server<-function(input,output,session){
State <- reactiveVal()
  observeEvent(event_data("plotly_click", source = "State"), {
    State(event_data("plotly_click", source = "State")$x)
  })
output$p1 <- renderPlotly({
  total %>% 
  count(input$state, wt = sales) %>%
  plot_ly(x = input$state, y = ~n, source = "State") %>%
  axis_titles() %>% 
  layout(title = "State")
  })}
runApp(list(ui = ui, server = server), launch.browser = TRUE)

the most obvious error is that p1 starts by using a total object, that is not defined anywhere
assuming you meant re, consider this smaller program, outside of shiny

library(tidyverse)

re<-tibble::tribble(
  ~Order.ID, ~Order_Date, ~CustomerName,             ~State,     ~City, ~sales,
  "B-26180",  "4/5/2017", "Bhaggyasree",   "Andhra Pradesh", "Udaipur",   880L,
  "B-26248",  "4/6/2017",       "Mohit",            "Bihar", "Udaipur",   121L,
  "B-26262",  "4/7/2017",    "Vaibhavi", "Himachal Pradesh", "Udaipur",    24L,
  "B-26280",  "4/8/2017",      "Akshay", "Himachal Pradesh", "Udaipur",   145L,
  "B-26162",  "4/8/2017",      "Mitali",        "Karnataka", "Udaipur",   133L,
  "B-26254",  "4/9/2017",     "Noshiba",        "Karnataka", "Udaipur",   312L,
  "B-26156",  "4/9/2017",   "Chayanika",   "Madhya Pradesh", "Udaipur",  1103L)

input <- list(state="Himachal Pradesh")
re %>% 
  count(input$state, wt = sales)

input <- list(state=c("Himachal Pradesh",
              "Bihar"))
re %>% 
  count(input$state, wt = sales)

# likely intension
re %>% filter(State %in% input$state) %>% count(State,wt=sales)

I didn't get what you did there, I want to enable the multiple selection so that user can select as much as option he wants. I beg your pardon for not getting it, I don't know how it will incorporate with my code.

Thanks for quick reply though

You have to think about the values that input$state might contain, and write code that expects that.

it is working fine here,then I must be doing something wrong in the previous code but unable to track it down, could you please point out where it is ,it would be real help.that code got quite complex due to it's nature of being reactive which is making it hard to figure it out.

Thanks

if (interactive()) {

library("shiny")
library("shinyWidgets")
re<-tibble::tribble(
      ~Order.ID, ~Order_Date, ~CustomerName,             ~State,     ~City, ~sales,
      "B-26180",  "4/5/2017", "Bhaggyasree",   "Andhra Pradesh", "Udaipur",   880L,
      "B-26248",  "4/6/2017",       "Mohit",            "Bihar", "Udaipur",   121L,
      "B-26262",  "4/7/2017",    "Vaibhavi", "Himachal Pradesh", "Udaipur",    24L,
      "B-26162",  "4/8/2017",      "Mitali",        "Karnataka", "Udaipur",   133L,
      "B-26254",  "4/9/2017",     "Noshiba",        "rajasthan", "Udaipur",   312L,
      "B-26156",  "4/9/2017",   "Chayanika",   "Madhya Pradesh", "Udaipur",  1103L)

ui <- fluidPage(
  selectInput(
    inputId = "id", label = "state :",
    choices = c(unique(re$State)),multiple = TRUE,
    selected = re$State[1], width = "350px"
  ),
  fluidPage(withLoader(plotlyOutput("res"),type = "html",loader = "loader2"))
)

server <- function(input, output, session) {
  output$res <- renderPlotly({
   plot_ly(x = input$id, y = input$sales,type="bar")
  })
}
shinyApp(ui = ui, server = server)
}

it is not working fine... in your new example
The correct code for your new example would include

  output$res <- renderPlotly({
    re_filter <- filter(re,
                         State %in% input$id)

    plot_ly(data = re_filter,
            x = ~State,
            y = ~sales,type="bar")
  })

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.