闪亮的服务器 - 安排mysql查询而不是每次加载应用程序时运行?

时间:2016-12-09 10:24:57

标签: mysql r azure shiny shiny-server

我在Azure中的Ubuntu VM中运行了一个Shiny Server。

如何每天晚上安排server.R中的mysql查询?如何在每次访问应用程序时避免运行它们?

这是我的服务器示例.R和ui.R:

server.R

library(shiny)
library(RMySQL)
library(ggplot2)
#library(ggiraph)
library(lubridate)

##Connect to Redmine db
con <- dbConnect(MySQL(),
                 user = '#',
                 password = '#',
                 host = '#',
                 dbname='#')

tickets<-dbGetQuery(con, "Select * from table")
issues_speed_unique<-unique(na.omit(dbGetQuery(con,"Select * from table2")))
dbDisconnect (con) 

some aggregations....

shinyServer(
  function(input,output){

ui.R

library(shiny)
library(ggplot2)
#library(ggiraph)
#library(htmltools)
library(lubridate)

shinyUI(fluidPage(  

1 个答案:

答案 0 :(得分:0)

使用code I found there

的这种改编来替换server.R中的开头代码
filename <- paste0(Sys.Date(),'.RData')
if (!file.exists(filename)){
    # don't have a cached copy so run the query
    library(RMySQL)
    con <- dbConnect(MySQL(),
                     user = '#',
                     password = '#',
                     host = '#',
                     dbname='#')

    tickets<-dbGetQuery(con, "Select * from table")
    issues_speed_unique<-unique(na.omit(dbGetQuery(con,"Select * from table2")))
    dbDisconnect (con) 

    # save the query results for the future
   save(list=c('tickets', 'issues_speed_unique'), file=filename)
   rm(list=c('tickets', 'issues_speed_unique') )
}
load(file=filename)