在R中使用reactPoll:checkFunc未执行

问题描述

我对R很陌生。我尝试使用reactPoll更新仪表板数据。我所有的数据都是从数据库提取的。该代码没有显示错误。但是仪表板不会像我设置的那样每天更新。这是我的代码

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)
 
    }
 )
}

我认为格式不错,因为没有错误显示。我还尝试查看控制台中的最长月份。但是,它说object not found基本上意味着checkFunc没有运行。我不知道这里出了什么问题。谢谢!

解决方法

步骤:

1-您需要在服务器内部创建反应式投票。 log_db

2- 在服务器(在您的情况下为:renderTable)内创建一个渲染对象,并在其中带有带有反括号的reactPoll:output$idforUi<- renderTable( { log_db() })

3-在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)

您无法从控制台访问它并不意味着checkFunc没有运行,您将无法访问控制台上的“ month”对象,因为它仅存在于reactpoll函数(局部变量)中,而不存在在全球环境中。 See this