ReactiveTimer与if语句R-Shiny [英] ReactiveTimer with if statement R-Shiny

查看:143
本文介绍了ReactiveTimer与if语句R-Shiny的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个在Linux服务器上运行的R-Shiny应用程序。
我需要的所有数据来自使用API​​和库(coinmarketcapr)的网站。

I've made a R-Shiny app which runs on a linux server. All the data which I need is coming from a website using API and the library(coinmarketcapr).

该应用程序每5分钟检索一次最新数据并保存它位于服务器上的csv文件中。

The app retrieves the latest data every 5 minutes and saves it in a csv-file on the server.

但现在我遇到的问题是每次重新加载页面时,应用程序都会检索新数据并且我总是有重复的我的时间序列中的值。

But now I have the problem that every time I reload the page, the app retrieves the new data and I always have duplicate values in my time series.

我使用ReactiveTimer,设置为5分钟。

I work with the ReactiveTimer, set to 5 minutes.

是否有ReactiveTimer检查csv文件中最后一个条目的时间并将其与当前系统时间进行比较的可能性?如果csv文件中的时间应该是系统时间的5分钟或更早,他应该执行代码,否则不执行。

Is there a possibility that the ReactiveTimer checks the time of the last entry in the csv-file and compares it with the current system time? If the time in the csv-file should be 5 minutes or older than the system time, he should execute the code, otherwise not.

我还想询问是否有人知道你是否可以运行一个闪亮的应用程序,而不是在浏览器中打开它,我。即代码是否一直在运行并收集数据?

I also wanted to ask if anyone knows if you can run a shiny app without having it open in the browser, i. e. the code is constantly running and collecting data?

编辑:

App on : http://srv-lab-t-416.zhaw.ch/有光泽/加密10 /

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函数现在都是在一个单独的文件中完成的,闪亮的应用程序只收集数据并显示它。

I solved the problem by using cronR. All the math an API functions now were made in a separate file and the shiny app only collects the data and displays it.

所以现在,我不必使用再一次使用ReactiveTimer。

So now, I don't have to use the ReactiveTimer anymore.

这篇关于ReactiveTimer与if语句R-Shiny的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆