简体   繁体   English

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

[英]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.我对 R 很陌生。我尝试使用 reactivePoll 来更新我的仪表板数据。 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.但是,它说object not found ,这基本上意味着 checkFunc 没有运行。 I wonder what goes wrong here.我想知道这里出了什么问题。 Thank you!谢谢!

Steps:脚步:

1-You need to create the reactivepoll inside the server. 1-您需要在服务器内部创建响应式轮询。 log_db日志数据库

2- Create a rendering object inside the server (in your case: renderTable) with reactivePoll inside with parentheses: output$idforUi<- renderTable( { log_db() }) 2-在服务器内创建一个渲染对象(在您的情况下:renderTable),其中带有括号的reactivePoll: output$idforUi<- renderTable( { log_db() })

3-Create the output for your render object in the ui. 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)

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

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

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