如何在闪亮的数据框中存储SQL查询结果

时间:2017-01-04 04:27:44

标签: r shiny

我是R的新手,我正在使用闪亮的软件包来构建一个可以采用不同类型查询的闪亮应用程序,这些查询采用用户在ui级别提供的动态用户ID,并且我还要显示结果的演示查询ui。 所以,我的问题是,我无法将查询结果存储到数据框中,同时单击“下载”按钮,csv不存储在我的系统中。我的代码如下。感谢。

###server

library(shiny)
library(RMySQL)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$queryset,
       "CDR" = cdr,
       "ASSET" = ast,
       "USAGE" = usg)
})
output$tbl <- renderTable({
conn <- dbConnect(drv = RMySQL::MySQL(),dbname = "xxxx",
host =   "xxxxxx",
username = "xxxxx",
password = "xxxxx"),
q<-dbSendQuery(conn,paste0("select * from table where user_id='",input$user_id,"' and start_time >= '2016-07-16' and start_time < '2016-07-28' order by start_time limit 10 ;",sep = ""
))
dat<- dbFetch(q,n=-1)
on.exit(dbDisconnect(conn), add = TRUE)
})
output$view <- renderTable({
head({dat}, n = input$nrows)
})
output$downloadData <- downloadHandler(
filename = function() { paste(input$user_id, '.csv', sep='') },
content = function(file) {
write.csv({dat}, file)
})
}
)

###ui
library(shiny)
shinyUI(fluidPage(
titlePanel("My App"),
sidebarLayout(
sidebarPanel(
selectInput("queryset", "Choose the type of query:", 
              choices = c("CDR", "ASSET", "USAGE")),
numericInput("nrows", "Enter the no. of observations:", 10),
numericInput("user_id", "Enter user_id:", 0),
downloadButton('downloadData', 'Download',class = NULL)
),
mainPanel(
tableOutput("view")
)
)
))

0 个答案:

没有答案