问题
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