【问题标题】:Storing reactive data in shiny from SQL从 SQL 中以闪亮的形式存储反应性数据
【发布时间】: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,我会去复制。那个应用程序很酷!我会从中学到很多,并希望实现一些功能。

标签: r shiny rmysql


【解决方案1】:

我想改变

values$df <- reactive({ rbind(values$df, wq() )   }) 

在您的新server.R

observe({
  values$df <- rbind(isolate(values$df), wq())
})

可能会解决您的问题。

编辑:这是一个使用本地连接的工作示例:

library(markdown)
library(RMySQL)
library(DBI)
library(sqldf)

con <- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "mtcars", mtcars)
query <-  function(...) dbGetQuery(con, ...) 

wq = data.frame()

ui <- shinyUI(fluidPage(
  titlePanel("Generic grapher"),
  sidebarLayout(
    sidebarPanel(

      numericInput("wafer", label = h3("Select number of cylinders:"),
                   value = NULL),

      actionButton("do", "An action button")
    ),

    mainPanel(
      verbatimTextOutput("value"),
      verbatimTextOutput("que"),
      verbatimTextOutput("wq_print"),
      dataTableOutput(outputId="pos")
    )
  )
)
)

server <- shinyServer(function(input, output){

  values <- reactiveValues()
  values$df <- data.frame()

  d <- eventReactive(input$do, { input$wafer })

  output$value <- renderPrint({ d() }) 

  a <- reactive({ paste("SELECT * FROM mtcars WHERE cyl = ", d(), sep="") })

  output$que <- renderPrint({ a() }) 

  observe({
    if (!is.null(d())) {
      wq <- reactive({  query( a() ) })

      output$wq_print <- renderPrint({ print(str(wq())) })

      values$df <- rbind(isolate(values$df), wq())
    }
  })

  output$pos <- renderDataTable({ values$df })  

})

shinyApp(ui, server)

对原始代码的相关更改是!is.null(d()) 条件,用于处理d() 的初始NULL 值,并在观察者内部使用values$df &lt;- rbind(isolate(values$df), wq())。希望这有助于修复您的代码!

【讨论】:

  • 感谢您的回复。尝试时出现错误:observe({ values$df
  • 啊等一下没看到隔离
  • ah仍然与isolate相同的错误:observe({ values$df
  • 您在代码中显示的dbConnect 调用是否正确?当我尝试复制您的示例时,连接到数据库时出现错误,然后是ERROR: [on_request_read] connection reset by peer
  • er dam,我应该省略细节:-s!但是是的,这是正确的......好的,需要尽快编辑!
猜你喜欢
  • 2019-10-12
  • 2015-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 2020-03-12
  • 2017-06-28
  • 1970-01-01
相关资源
最近更新 更多