【发布时间】:2015-07-16 08:18:33
【问题描述】:
这是这个问题的后续:
Acessing SQL database using shiny with reactive query
我正在尝试使用闪亮的应用程序从从 SQL 数据库获取的数据构建数据框。目前我能够查询数据库并返回一组数据。现在我想将该数据保存到数据框中,然后从后续查询中添加更多数据。这是我的代码:
用户界面
library(markdown)
shinyUI(fluidPage(
titlePanel("Generic grapher"),
sidebarLayout(
sidebarPanel(
numericInput("wafer", label = h3("Select wafer ID:"), value = NULL),
actionButton("do", "An action button")
),
mainPanel(
verbatimTextOutput("value"), verbatimTextOutput("que"), dataTableOutput(outputId="pos")
)
)
)
)
服务器:
library(RMySQL)
library(DBI)
library(sqldf)
con = dbConnect(RMySQL::MySQL(), dbname="xx", username="pete", password="xx", host="xx", port=3306)
query <- function(...) dbGetQuery(con, ...)
wq = data.frame()
shinyServer(function(input, output){
d <- eventReactive(input$do, { input$wafer })
output$value <- renderPrint({ d() })
a <- reactive({ paste("Select id from wafer where wafer_id=",d(), sep="") })
output$que <- renderPrint({ a() })
wq <- reactive({ query( a() ) })
output$pos <- renderDataTable({ wq() })
})
现在我正在尝试使用这两个答案中的信息将每次搜索的数据存储在数据框中:
Add values to a reactive table in shiny
What's the difference between Reactive Value and Reactive Expression?
新服务器:
library(RMySQL)
library(DBI)
library(sqldf)
con = dbConnect(RMySQL::MySQL(), dbname="xx", username="pete", password="xx", host="xx", port=3306)
query <- function(...) dbGetQuery(con, ...)
wq = data.frame()
shinyServer(function(input, output){
values <- reactiveValues()
values$df <- data.frame()
d <- eventReactive(input$do, { input$wafer })
output$value <- renderPrint({ d() })
a <- reactive({ paste("Select id from wafer where wafer_id=",d(), sep="") })
output$que <- renderPrint({ a() })
wq <- reactive({ query( a() ) })
values$df <- reactive({ rbind(values$df, wq() ) })
output$pos <- renderDataTable({ values$df })
})
但是,当我这样做时,数据表永远不会在我的应用程序中呈现。我没有错误消息。任何想法我哪里出错了?任何帮助表示赞赏!
【问题讨论】:
-
我刚刚发布了一个示例应用程序:github.com/MarkEdmondson1234/ga-dashboard-demo
-
查看server.r中的“eventTable”和“eventData”,以及functions.r中的MySQL函数,例如loadData()
-
谢谢 MarkeD,我会去复制。那个应用程序很酷!我会从中学到很多,并希望实现一些功能。