0

A little app to filter sales by company

library(dplyr)
library(tidyr)
library(DT)

company=c("A","L","S","T","W","A","L","S","T","W")
sales=c(120,140,160,180,200,120,140,160,180,200)

server <- function(input, output) {

  output$theCustomersList <- renderUI({
    list(
      selectInput("customer", "Choose a customer:",
                   c("A","L","S","T","W")
                   ,selectize=FALSE
                   ,multiple=TRUE
                   ,selected="A"
                   )
        )})

  output$result <- DT::renderDataTable(data_frame(company,sales) %>% filter(company%in%input$customer))
    }

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      uiOutput("theCustomersList")
    ),


    mainPanel(DT::dataTableOutput('result'))
  )
)

shinyApp(ui = ui, server = server)

This works OK as is but if I change it to use a postgresql table as the source of the sales data using src_postgresql I get an error if I only select a single company from the dropdown.

Basically the SQL generated gives the following

Error: RS-DBI driver: (could not Retrieve the result : ERROR:  syntax error at or near "'A'" LINE 4: ... AND "company" IN 'A'

Anybody with any ideas?

Update.... this is definitely a database issue. When the filter is applied to an in memory dataframe the "%in%" construct works with either a single selection or multiple selections. When applied to the postgres source the brackets are missed off the resulting sql meaning that the SQL is malformed.

RS-DBI driver: (could not Retrieve the result : ERROR:  syntax error at or near "'A'"
LINE 3: WHERE "company" IN 'A'  
4
  • Please post your sql command. The error seems to be related to sql only and not related to shiny. Commented Apr 26, 2016 at 18:25
  • Hi... I'm not generating the SQL but from the error I think its generating "company" IN ("A","L") for multiple selections and "company" IN "A" for a single - that is, it is missing out the brackets. Commented Apr 26, 2016 at 19:16
  • Answer here stackoverflow.com/questions/38251927/… Commented Aug 4, 2016 at 14:44
  • Closing because solutions are here stackoverflow.com/questions/37591084/… stackoverflow.com/questions/38251927/… Commented Jul 1, 2017 at 13:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.