invalidateLater在R闪亮的应用程序中停止

时间:2017-02-08 09:34:10

标签: r shiny

我正在写一个闪亮的应用程序,并且有一个情节每10秒更新一次。该应用程序运行良好,它正在更新。然而,经过一些更新,即大约30次,它没有任何理由停止。

用于图表更新:

invalidateLater

请您告诉我应该怎么做?

library("shiny")
library("shinythemes")
library("ggplot2")

## generating the time of the system
t <- Sys.time()
n <- 101 # some time lage
df <- data.frame(c(1:1000), runif(1000, 0, 1) )  # in addition, df is just a dataframe in the memory

shinyUI(

  tabPanel("Home", plotOutput(outputId = "plot0") )

)


shinyServer(function(input, output, session) {

  output$plot0 <- renderPlot({  # Signal realtime View
  invalidateLater(500, session) # updating the plot each 500 miliseconds

  n <- as.integer(Sys.time() - t) + n # updating the new elements which should be visualized
  ggplot() + geom_line(aes(x = df[((n-100) :n), 1], y = df[((n-100) :n) , 2]     ), colour = "blue") +      
  xlab("Time [s]") + ylab("Channel") # normal ggplot :-)

  })
})

1 个答案:

答案 0 :(得分:1)

您的时差计算不会考虑差异在几秒,几分钟或几小时的位置,因此在60秒后差异将为1.

尝试这样的事情:

#rm(list = ls())
library(shiny)
library("ggplot2")
t <- Sys.time()
n <- 101 # some evaluation
df <- data.frame(c(1:1000), c(1:1000) )

ui<-  shinyUI(pageWithSidebar(
  headerPanel("Distribution analysis"),
  sidebarPanel(),
  mainPanel(
    plotOutput(outputId = "plot0"))
))

server<- shinyServer(function(input, output,session) {

  mydata <- reactive({
    invalidateLater(300, session) # updating the plot each 300 miliseconds
    n <- as.integer(difftime(Sys.time(),t, units = "secs")) + n # updating the new elements which should be visualized
    df[((n-100) :n),]
  })

  output$plot0 <- renderPlot({  # Signal realtime View
    ggplot() + geom_line(aes(x = mydata()[,1], y = mydata()[,2]), colour = "blue") +      
      xlab("Time [s]") + ylab("Channel") # normal ggplot :-)
  })
})  
shinyApp(ui=ui, server=server) 

enter image description here