繁体   English   中英

在 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-您需要在服务器内部创建响应式轮询。 日志数据库

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没有运行,您将无法访问控制台上的“月”对象,因为它仅存在于reactivepoll函数(局部变量)中,而不存在于全局环境中. 看到这个

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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