Shiny APP显示最后检索到的mysql数据

时间:2017-10-22 19:11:38

标签: r shiny shiny-server

下面是我的Shiny代码,我正在连接到远程mysql db。我第一次得到正确的结果然后当我在mysql中有新行时db闪亮仍然显示旧数据,即使我在不​​同的浏览器中运行。

#load libraries

library(stringr)
library(twitteR)
library(plyr)
library(ROAuth)
library(base64enc)
library(shiny)
require(RJSONIO)
library(RMySQL) # will load DBI as well

options(shiny.sanitize.errors = FALSE)

lapply( dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)

con <- dbConnect(dbDriver("MySQL"), user = "user_name", password = "passowrd", dbname = "db_name", host="remotehost.org")

on.exit(dbDisconnect(con))

将数据保存在测试变量

test <- dbGetQuery(con, paste("SELECT * FROM tablename WHERE columnname = '","'"))

#在浏览器上显示数据

ui <- fluidPage(textOutput(outputId="jsonoutput"))

server <- function(input, output) {
  output$jsonoutput <- renderText({
    toJSON(list(NLP =  test))
  })


}

dbDisconnect(con)

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

我认为您需要将查询放在服务器函数中,否则它只在应用程序初始化时运行。您还可以添加一个actionButton。

ui <- fluidPage(
    actionButton("action","Action Button"),
    textOutput(outputId="jsonoutput")
)

server <- function(input, output) { 
  test <- eventReactive(input$action,{ dbGetQuery(con, paste("SELECT * FROM tablename WHERE  
                                           columnname = '","'"))  
  })  
  output$jsonoutput <- renderText({
            toJSON(list(NLP =  test()))
            }) 
}