Passing multiple selections from selectizeInput to MySQL query

天涯浪子 提交于 2019-12-11 19:14:13

问题


I am trying to pass multiple values of a selectizeInput to a MySQL query.

The question is what is the right syntax for this kind of operation in a shiny app ?

What I tried and is working with one value

  library(shiny)
  library(DBI)
  library(RMySQL)

  server <- shinyServer(function(input, output, session) {
            con <- dbConnect(MySQL(), user='user', port = 3306, 
   password='pwd', dbname='db', host='host' )

           on.exit(dbDisconnect(con), add = TRUE) 

  output$textview <- renderUI({

         con <- dbConnect(MySQL(), user='user', port = 3306, password='pwd', 
               dbname='db', host='host' )

         on.exit(dbDisconnect(con), add = TRUE)


       text <- reactive({
                dbGetQuery(con, statement = 
                paste0(" SELECT author,  title, publicationDate,  FROM publications  
               WHERE publications.year LIKE %'",input$year,"'% ")
             )
               })

                text <-text()
                HTML(text)

          })




   session$onSessionEnded(function() { dbDisconnect(con) })
     })


 ui_panel <- 
      tabPanel("Multi-Select Input Test",
        sidebarLayout(
           sidebarPanel( 


         selectizeInput('year', 'Select Year of publication:', choices = 
         publications.year, multiple = TRUE  options = list(maxOptions = 5)
         ),

                   br(),
                   submitButton("Update Text View"),
                   br()
       ),
       mainPanel(
       tabsetPanel(tabPanel("Text",htmlOutput("textview"))

       )
     )
))


 ui <- shinyUI(navbarPage(" ",ui_panel))

 runApp(list(ui=ui,server=server))

What is the correct syntax in the MySQL command that will allow me to pass more than one value from selectizeInput (input$year)? I tried using IN instead of LIKE as below but it did not work

    text <- reactive({
                dbGetQuery(con, statement = 
                paste0(" SELECT author,  title, publicationDate,  FROM 
                publications WHERE publications.year IN %'",input$year,"'% ")
             )
               })

回答1:


You need to construct a SQL similar to this:

SELECT * FROM publications WHERE year IN (2016, 2017)

This should generate that:

text <- reactive({
  year_selected <- paste(input$year, collapse = ',')
  sql = paste("SELECT * FROM publications WHERE year IN (",year_selected,")")
  dbGetQuery(con, statement = sql)
})

A minimal Shiny App:

library(shiny)
ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
        selectizeInput('year', 'Select Year of publication:',
                       choices = c(2017L, 2018L),
                       multiple = TRUE,
                       options = list(maxOptions = 5)
        )
      ),
      mainPanel(
        verbatimTextOutput('text')
      )
   )
)

server <- function(input, output) {
   output$text <- renderText({
     library(glue)
     year_selected <- paste(input$year, collapse = ',')
     glue("SELECT * FROM publications WHERE year IN ({year_selected})")
   })
}

shinyApp(ui = ui, server = server)


来源:https://stackoverflow.com/questions/52090142/passing-multiple-selections-from-selectizeinput-to-mysql-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!