簡體   English   中英

帶有if語句R-Shiny的ReactiveTimer

[英]ReactiveTimer with if statement R-Shiny

我做了一個運行在Linux服務器上的R-Shiny應用程序。 我需要的所有數據都來自使用API​​和庫(coinmarketcapr)的網站。

該應用程序每5分鍾檢索一次最新數據,並將其保存在服務器上的csv文件中。

但是現在我遇到的問題是,每次重新加載頁面時,應用程序都會檢索新數據,並且我的時間序列中始終有重復的值。

我使用ReactiveTimer,設置為5分鍾。

ReactiveTimer是否有可能檢查csv文件中最后一個條目的時間並將其與當前系統時間進行比較? 如果csv文件中的時間應比系統時間晚5分鍾或更長時間,則他應執行代碼,否則不執行。

我還想問問是否有人知道您是否可以在不在瀏覽器中打開的情況下運行閃亮的應用程序,即代碼是否一直在運行並收集數據?

編輯:

應用程式: http//srv-lab-t-416.zhaw.ch/shiny/Crypto10/

R代碼:

autoInvalidate3 <- reactiveTimer((5*60*1000), session)

          Crypto10 <- reactive({
                    Crypto10 <- rep(NA,length(Index.Daten$Date))
                    z = 0
                    for(i in length(Index.Daten$Date):1){
                              z = z +1

                              # Price
                              P.i = c(Index.Daten$BTC.Price[i],Index.Daten$BCH.Price[i],Index.Daten$ETH.Price[i],Index.Daten$DASH.Price[i],Index.Daten$MIOTA.Price[i],
                                      Index.Daten$LTC.Price[i],Index.Daten$XMR.Price[i],Index.Daten$XEM.Price[i],Index.Daten$NEO.Price[i],Index.Daten$XRP.Price[i])

                              P.0 = c(Index.Daten$BTC.Price[length(Index.Daten$Date)],Index.Daten$BCH.Price[length(Index.Daten$Date)],Index.Daten$ETH.Price[length(Index.Daten$Date)],
                                      Index.Daten$DASH.Price[length(Index.Daten$Date)],Index.Daten$MIOTA.Price[length(Index.Daten$Date)],
                                      Index.Daten$LTC.Price[length(Index.Daten$Date)],Index.Daten$XMR.Price[length(Index.Daten$Date)],
                                      Index.Daten$XEM.Price[length(Index.Daten$Date)],Index.Daten$NEO.Price[length(Index.Daten$Date)],Index.Daten$XRP.Price[length(Index.Daten$Date)])
                              # Coins
                              Q.i = c(Index.Daten$BTC.Coins[i],Index.Daten$BCH.Coins[i],Index.Daten$ETH.Coins[i],Index.Daten$DASH.Coins[i],Index.Daten$MIOTA.Coins[i],
                                      Index.Daten$LTC.Coins[i],Index.Daten$XMR.Coins[i],Index.Daten$XEM.Coins[i],Index.Daten$NEO.Coins[i],Index.Daten$XRP.Coins[i])


                              # MK.i = Preis.i * Q.i
                              MK.i = P.i*Q.i

                              # MK.0 = Preis.0 * Q.i
                              MK.0 = P.0*Q.i

                              Crypto10[z] = (sum(MK.i))/((sum(MK.0))/100)
                    }
                    return(Crypto10)
          })

          output$C10 <- renderDygraph({
                    q <- data.frame(Index.Daten$Date,rev(Crypto10()))
                    dygraph(xts(q[,-1], order.by=q[,1]), xlab = "Time", ylab = "Indexpunkte") %>% dyRangeSelector()
          })

          Crypto10.V3 <- reactive({
                    autoInvalidate3()
                    if(!file.exists(paste(path,"Index.Daten.V3.csv", sep = ""))){

                              Index.Daten.V3 = data.frame(Date = NA
                                                          ,BTC.Price = NA,BTC.Coins = NA
                                                          ,BCH.Price = NA,BCH.Coins = NA
                                                          ,ETH.Price = NA,ETH.Coins = NA
                                                          ,DASH.Price = NA,DASH.Coins = NA
                                                          ,MIOTA.Price = NA,MIOTA.Coins = NA
                                                          ,LTC.Price = NA,LTC.Coins = NA
                                                          ,XMR.Price = NA,XMR.Coins = NA
                                                          ,XEM.Price = NA,XEM.Coins = NA
                                                          ,NEO.Price = NA,NEO.Coins = NA
                                                          ,XRP.Price = NA,XRP.Coins = NA)

                              Sys.setenv(TZ='CET')
                              Index.Daten.V3$Date = Sys.time()

                              write.csv(Index.Daten.V3,(paste(path,"Index.Daten.V3.csv", sep = "")))

                    } else{

                              # Index.Daten.V3 einlesen
                              Index.Daten.V3 = subset(read.csv(paste(path,"Index.Daten.V3.csv", sep = ""),sep = ","),select = -X)
                              Index.Daten.V3$Date = as.POSIXct(Index.Daten.V3$Date)
                    }

                    API.BTC = API.all()[which(API.all()$symbol == "BTC"),]
                    API.BCH = API.all()[which(API.all()$symbol == "BCH"),]
                    API.ETH = API.all()[which(API.all()$symbol == "ETH"),]
                    API.DASH = API.all()[which(API.all()$symbol == "DASH"),]
                    API.MIOTA = API.all()[which(API.all()$symbol == "MIOTA"),]
                    API.LTC = API.all()[which(API.all()$symbol == "LTC"),]
                    API.XMR = API.all()[which(API.all()$symbol == "XMR"),]
                    API.XEM = API.all()[which(API.all()$symbol == "XEM"),]
                    API.NEO = API.all()[which(API.all()$symbol == "NEO"),]
                    API.XRP = API.all()[which(API.all()$symbol == "XRP"),]

                    if(is.na(Index.Daten.V3$BTC.Price[length(Index.Daten.V3$BTC.Price)])){

                              p = length(Index.Daten.V3$Date)

                              Index.Daten.V3[p,2:21] = c(
                                        as.numeric(API.BTC$price_usd),as.numeric(API.BTC$available_supply),
                                        as.numeric(API.BCH$price_usd),as.numeric(API.BCH$available_supply),
                                        as.numeric(API.ETH$price_usd),as.numeric(API.ETH$available_supply),
                                        as.numeric(API.DASH$price_usd),as.numeric(API.DASH$available_supply),
                                        as.numeric(API.MIOTA$price_usd),as.numeric(API.MIOTA$available_supply),
                                        as.numeric(API.LTC$price_usd),as.numeric(API.LTC$available_supply),
                                        as.numeric(API.XMR$price_usd),as.numeric(API.XMR$available_supply),
                                        as.numeric(API.XEM$price_usd),as.numeric(API.XEM$available_supply),
                                        as.numeric(API.NEO$price_usd),as.numeric(API.NEO$available_supply),
                                        as.numeric(API.XRP$price_usd),as.numeric(API.XRP$available_supply)
                              )

                              # Index.Daten.V3 als CSV speichern
                              write.csv(Index.Daten.V3,(paste(path,"Index.Daten.V3.csv", sep = "")))

                    }else{

                              p = length(Index.Daten.V3$Date)+1

                              Index.Daten.V3[p,2:21] = c(
                                        as.numeric(API.BTC$price_usd),as.numeric(API.BTC$available_supply),
                                        as.numeric(API.BCH$price_usd),as.numeric(API.BCH$available_supply),
                                        as.numeric(API.ETH$price_usd),as.numeric(API.ETH$available_supply),
                                        as.numeric(API.DASH$price_usd),as.numeric(API.DASH$available_supply),
                                        as.numeric(API.MIOTA$price_usd),as.numeric(API.MIOTA$available_supply),
                                        as.numeric(API.LTC$price_usd),as.numeric(API.LTC$available_supply),
                                        as.numeric(API.XMR$price_usd),as.numeric(API.XMR$available_supply),
                                        as.numeric(API.XEM$price_usd),as.numeric(API.XEM$available_supply),
                                        as.numeric(API.NEO$price_usd),as.numeric(API.NEO$available_supply),
                                        as.numeric(API.XRP$price_usd),as.numeric(API.XRP$available_supply))

                              Sys.setenv(TZ='CET')
                              Index.Daten.V3$Date[p] = Sys.time()

                              # Index.Daten.V3 als CSV speichern
                              write.csv(Index.Daten.V3,(paste(path,"Index.Daten.V3.csv", sep = "")))
                    }

我通過使用cronR解決了這個問題。 現在,所有API函數的數學運算都在一個單獨的文件中進行,而閃亮的應用程序僅收集並顯示數據。

所以現在,我不再需要使用ReactiveTimer。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM