IN SQL operator in R-Shiny
- by Piyush
I am taking multiple selection for component as per below code.
selectInput("cmpnt",
"Choose Component:",
choices = as.character(levels(Material_Data()$CMPNT_NM)),multiple = TRUE)
But I am trying to write a sql statement as given below, then its not working. Neither it is throwing any error message. When I was selecting one option at a time (without mutiple = TRUE) then it was working (since I was using "=" operator). But after using "multiple=TRUE" I need to use IN operator, which is not working.
Input_Data2 <- fn$sqldf( paste0( "select * from Input_Data1 where MTRL_NBR = '$mtrl1' and CMPNT_NM in ('$cmpnt1')") )
Thanks in advance for any help on this.
Thanks jdharrison! Pleasefind the detailed code:
# server.R
library(RODBC)
library(shiny)
library(sqldf)
Input_Data <- readRDS("InputSource.rds")
Mtrl <- factor(Input_Data$MTRL_NBR)
Mtrl_List <- levels(Mtrl)
shinyServer(function(input, output) {
# First UI input (Service column) filter clientData
output$Choose_Material <- renderUI({
if (is.null(clientData()))
return("No client selected")
selectInput("mtrl",
"Choose Material:",
choices = as.character(levels(clientData()$MTRL_NBR)),
selected = input$mtrl
)
})
# Second UI input (Rounds column) filter service-filtered clientData
output$Choose_Component <- renderUI({
if(is.null(input$mtrl))
return()
if (is.null(Material_Data()))
return("No service selected")
selectInput("cmpnt",
"Choose Component:",
choices = as.character(levels(Material_Data()$CMPNT_NM)),multiple = TRUE)
})
# First data load (client data)
clientData <- reactive({
# get(input$Input_Data)
return(Input_Data)
})
# Second data load (filter by service column)
Material_Data <- reactive({
dat <- clientData()
if (is.null(dat))
return(NULL)
if (!is.null(input$mtrl)) # !
dat <- dat[dat$MTRL_NBR %in% input$mtrl,]
dat <- droplevels(dat)
return(dat)
})
output$Choose_Columns <- renderUI({
if(is.null(input$mtrl))
return()
if(is.null(input$cmpnt))
return()
colnames <- names(Input_Data)
checkboxGroupInput("columns", "Choose Columns To Display The Data:",
choices = colnames,
selected = colnames)
})
output$text <- renderText({ print(input$cmpnt) })
output$data_table <- renderTable({
if(is.null(input$mtrl))
return()
if (is.null(input$columns) || !(input$columns %in% names(Input_Data)))
return()
Input_Data1 <- Input_Data[, input$columns, drop = FALSE]
cmpnt1 <- input$cmpnt
mtrl1 <- input$mtrl
Input_Data2 <- fn$sqldf( paste0( "select * from Input_Data1 where MTRL_NBR = '$mtrl1' and CMPNT_NM in ('$cmpnt1')") )
head(Input_Data2, 10)
})
})