Reputation: 10483
I have a dashboard that relies on 25+ database tables. I want to refresh this data periodically. Shiny reactivePoll
that checks row count on each table, and then reads the table when row count increases is the correct thing for my case.
But, I don't want to write 25+ reactivePoll
methods, one for each table. Instead, I want to define a 'generic' one and re-use it by simply supplying the database table name.
I can't find a way to do it. Any ideas? Conceptually, here is what I would like:
pollData <- reactivePoll(60000, session,
checkFunc = function() {
dbconn <- dbConnect(MySQL(), group = 'mysql')
query <- dbSendQuery(dbconn,
'
SELECT
MAX(timeCreated) as lastCreated
FROM
<table name passed as argument>;
')
lastFeedback <- dbFetch(query, -1)
dbClearResult(query)
dbDisconnect(dbconn)
lastFeedback$lastCreated
},
# This function returns the content of the logfile
valueFunc = function() {
dbconn <- dbConnect(MySQL(), group = 'mysql')
query <- dbSendQuery(dbconn,
'
SELECT
*
FROM
<table name passed as argument>
')
data <- dbFetch(query, -1)
dbClearResult(query)
dbDisconnect(dbconn)
data
}
)
server <- function(input, output, session) {
table1Data <- pollData(<table1 name>)
table2Data <- pollData(<table2 name>)
....
....
}
NOTE: I understand that I can move the checkFunc
and valueFunc
out and make them generic, however, it will still require 25+ reactivePoll() definitions that invoke these other two functions. That is what I am trying to avoid.
Upvotes: 3
Views: 1142
Reputation: 229
I found a more general answer at this question: Create a reactive function outside the shiny app
It cites: https://shiny.rstudio.com/articles/modules.html I believe you are trying to build a shiny module. In your case it could look something like follows (a function which calls a reactive function):
my.reactivePoll <- function(session, table_name, timeout = 60000)
{
my.dataframe <- reactivePoll(timeout, session,
checkFunc = function() {
lastFeedback <- "testValue"
# checking logic here
lastFeedback
},
# This function returns the content of the logfile
valueFunc = function() {
data <- paste0("New value", table_name)
# value logic here
data
})
my.dataframe
}
Then make calls in the server function, supplying the table_name
server <- function(input, output, session) {
table1Data <- my.reactivePoll(session, "table1")
table2Data <- my.reactivePoll(session, "table2")
}
Upvotes: 4