在 R 中使用 reactivePoll:checkFunc 没有执行

Use reactivePoll in R: The checkFunc didn't execute

我是 R 的新手。我尝试使用 reactivePoll 来更新我的仪表板数据。我所有的数据都是从数据库中提取的。代码显示没有错误。但是仪表板并没有像我设置的那样按天更新。这是我的代码:

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-您需要在服务器内部创建 reactivepoll。 log_db

2- 在服务器内部创建一个渲染对象(在你的例子中:renderTable),里面有 reactivePoll 和括号: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”对象,因为它仅存在于 reactivepoll 函数中(局部变量),不在全局环境中。 See this