使用数据库查询的R闪亮shinysky自动完成

时间:2016-06-23 00:09:23

标签: r shiny rmysql shinysky

我正在尝试使用列表来执行此自动完成,该列表将在每次应用程序加载时从数据库动态填充。不幸的是,以下方法无效。

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)

任何帮助都会很棒。

0 个答案:

没有答案