简体   繁体   中英

Use reactivePoll in R: The checkFunc didn't execute

I am quite new to R. I tried to use reactivePoll to update my dashboard data. All my data is drawn from the database. The code shows no error. But the dashboard is not updated by day as I set it. Here is my code:

log_db <- reactivePoll(60000*60*24, session,
                             # Check for maximum month
                             checkFunc = function() {

                                    #connect to the database
                                    #check for maximum month in the database. If there's a change, the value function will run. 
                                    maxmonth <- paste("SQL code")
                                    month <- dbGetQuery(maxmonth)
                                    return(month)

    },

    # Pull new table if value has changed
    valueFunc = function() {
      #connect to db
      #pull new dataframe,
      return(oldnew_combined)
 
    }
 )
}

I think the format is fine since there are no error shows. I also tried to see the maximum month in the console. However, it says object not found which basically means the checkFunc didn't run. I wonder what goes wrong here. Thank you!

Steps:

1-You need to create the reactivepoll inside the server. log_db

2- Create a rendering object inside the server (in your case: renderTable) with reactivePoll inside with parentheses: output$idforUi<- renderTable( { log_db() })

3-Create the output for your render object in the ui. ui=fluidPage(tableOutput("idforUi"))

library(shiny) # good practices
library(RMariaDB) #good practices

server <- function(input, output,session) {
#The connection to SQL does not need to be inside the checkfunction or valuefunction, 
#if you put it inside the checkfunction it will connect every milliseconds argument.
#If you place the connection inside the server but outside the reactivePoll, when you open the app it connects, and updates every milliseconds inside the reactivePoll

 localuserpassword="yourpassword"
 storiesDb<- dbConnect(RMariaDB::MariaDB(), user='YOUR_USER', password=localuserpassword, dbname='DBNAME', host='YOURHOST')
  
#your database will be checked if it changes every 60000 * 60 * 24 milliseconds (24 hours)
  log_db <- reactivePoll(60000*60*24, session, #reactivePoll inside the server
                         # Check for maximum month
                         checkFunc = function() {
                           
                        query2= "SELECT *  FROM YOURTABLE"
                           rs = dbSendQuery(storiesDb,query2)
                           dbFetch(rs)# visualize 
                           
                           
                         },
                         
                         # Pull new table if value has changed
                         valueFunc = function() {
                          query2= "SELECT *  FROM YOURTABLE"
                           rs = dbSendQuery(storiesDb,query2)
                           dbFetch(rs)# visualize 
                         }
  )

  
  #log_db is a function dont forget the () inside renderTable() 
  output$idforUi<- renderTable( { log_db() }) # renderTable
  #create a object to send the result of your reactivepoll for User Interface
  
  }
       # table output
ui=fluidPage(tableOutput("idforUi")) 
# Receive the result of your reactivepoll in the User Interface

shinyApp(ui, server)

You are unable to access it from the console does not mean that checkFunc did not run,you will not be able to access the "month" object on the console because it exists only in the reactivepoll function(local variable), not in global environment. See this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM