簡體   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