R shiny shinysky autocomplete using database query

444 views Asked by At

I am trying to do this autocomplete using a list which would be populated dynamically from a database everytime the applications loads. Unfortunately the following approach isn't working.

rm(list = ls())

library(shinysky)
library(shiny)
library(RMySQL)

loadData <- function(publisherId) {
# Connect to the database
mydb = dbConnect(MySQL(), user='root', password='root',host='localhost',dbname="tq")

# Construct the fetching query
  query <- sprintf("select * from tq.publisher_dim where publisher_id = %s",publisherId)  # Submit the fetch query and disconnect
  data <- dbGetQuery(mydb, query)
  dbDisconnect(mydb)
  data
}

loadPubIds <- function() {
  # Connect to the database
  mydb = dbConnect(MySQL(), user='root', password='root', host='localhost',     dbname="tq")

  # Construct the fetching query
  query <- sprintf("select distinct publisher_id from tq.publisher_dim" )  
#    Submit the fetch query and disconnect
  data <- dbGetQuery(mydb, query)
  dbDisconnect(mydb)
  data
}


my_autocomplete_list <- c(loadPubIds())

ui <- fluidPage(
  select2Input("txt","",choices = NULL,selected = NULL),
  textInput(inputId ="publisherId", label="choose publisherId", value = "",     width = NULL, placeholder = NULL),
  actionButton("goButton", "Go!"),
  dataTableOutput('mytable')
  #textOutput('myquery')
)

server <- function(input, output,session) {
  test <- reactive({
    pubs <- loadPubIds()  
  })
  observe({
    pubs <- loadPubIds()
    updateSelect2Input(session, 'txt', choices = as.list(pubs), label = "")
  })
  output$mytable <- renderDataTable({
    if (input$goButton == 0)
      return()

    isolate({ loadData(input$publisherId) })

  })


}

shinyApp(ui = ui, server = server)

Any help would be great.

0

There are 0 answers