How to update data automatically in shiny dashboard from mysql database?

浪尽此生 提交于 2021-02-10 16:49:25

问题


I want refresh data every hour in my shiny dashboard. So that the graphs in shinydashboard automatically update hourly basis.

Is there any source where I can learn or any sample code to practice ?

Thank you.


回答1:


I have got this with invalidateLater function. In the below code i just tried to get distinct names from a MySQL table. After running the shinyApp for first time, i have added a unique row to the same table so that after the specified time(i have checked with 60000 ms) we can check whether shiny is getting refreshed or not. It should display the newly entered name after that specified time. ( Change it to 3600000 ms for your requirement.)

library(shiny)
library(DBI)
library(pool)
pool <- dbPool(drv = RMySQL::MySQL(),dbname = "database",host = "localhost",username = "username",password = "password", port = 3306, unix.sock = "/var/run/mysqld/mysqld.sock")

ui <- fluidPage(
  uiOutput("names")
)

server <- function(input, output, session){
  getNames <- function(x){
    dbGetQuery(x, "SELECT DISTINCT names from dummyTable;")
  }

  refreshData <- reactive({
    invalidateLater(60000, session) 
    getNames(pool)
  })
  output$names <- renderUI({
    selectInput(inputId = "name", label = "First names", choices = c(as.character(refreshData()[,1])))
  })  
}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/52961100/how-to-update-data-automatically-in-shiny-dashboard-from-mysql-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!