I am importing some data from a MySQL table, but so far for checking the data updates I was just reloading the app. I would like for this to happen dynamically but I am not sure how to implement the invalidateLater function on my code or if I should even go for it, because I am seeing people recommending ReactivePoll. Basically I am receiving data into my database from 5 to 5 minutes and I want to check the database in a certain interval too and update the number showed in my Value Box.
Thanks!
shinyServer(function(input, output, session) {
conn <- dbConnect(RMySQL::MySQL(),
dbname="db",
host="localhost",
user="root",
password="password")
table<- dbReadTable(conn = conn, name ='table1', value= as.data.frame(table))
prd_number <- dbFetch(dbSendQuery(conn, "SELECT obj FROM table1 ORDER BY id DESC LIMIT 1"))
output$prdnumber <- shinydashboard::renderValueBox({
shinydashboard::valueBox(prd_number, "Number of products", icon=icon("tags"), color="purple"
)
})
})
UPDATE
I accomplished what I wanted but now I have:
- a curiosity: is it necessary to define a new reactivePoll for each different query? (eg: a reactivePoll function for getting a certain field from the db and another reactivePoll function for getting the whole table from the db)
- an error: more exactly unrecognized MySQL field type 7 in column 0 imported as character . I know this is due to the fact that I have timestamps in my table, but I not sure how to deal with them. I saw one solution might be to convert it to a UNIX timestamp but i want to print the date to the dashboard so it's not a good choice.
server.R
prd_number <- reactivePoll(20000, session, checkFunc = function(){
conn <- dbConnect(RMySQL::MySQL(),
dbname="db",
host="localhost",
user="root",
password="password")
val1 <- dbGetQuery(conn, "SELECT obj FROM table ORDER BY id DESC LIMIT 1")
val2 <- dbGetQuery(conn, "SELECT date FROM table ORDER BY id DESC LIMIT 1")
output$prdnumber <- shinydashboard::renderValueBox({
shinydashboard::valueBox(val1, "Number of items", icon=icon("tags"), color="purple"
)
})
output$update <- shinydashboard::renderValueBox({
shinydashboard::valueBox(val2, "Last update", icon=icon("tags"), color="lime"
)
})
dbDisconnect(conn)
})