R Plotly extendTraces:更改为增量和清除数据

R Plotly extendTraces:更改为增量和清除数据,r,shiny,plotly,R,Shiny,Plotly,我正在Plotly上试用extendTraces的R流媒体示例。我正在尝试向图表中添加一项功能,以便在浏览器在一段时间后开始暂停时清除所有数据(例如,actionButton等)。是否有办法在第二次单击actionButton时停止跟踪并清除跟踪/数据?或者,是否可以使图表递增,这样整个数据就不会存储在本地 库(闪亮) 图书馆(绘本) rand嗨,也许你可以这样做 library(shiny) library(plotly) library(shinyjs) rand <- functi

我正在Plotly上试用extendTraces的R流媒体示例。我正在尝试向图表中添加一项功能,以便在浏览器在一段时间后开始暂停时清除所有数据(例如,actionButton等)。是否有办法在第二次单击actionButton时停止跟踪并清除跟踪/数据?或者,是否可以使图表递增,这样整个数据就不会存储在本地

库(闪亮)
图书馆(绘本)

rand嗨,也许你可以这样做

library(shiny)
library(plotly)
library(shinyjs)
rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(
  # includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly: Multiple Traces", align = "center")),
  br(),
  div(actionButton("button", "Extend Traces"),actionButton("buttonReset", "Reset Traces"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph'),
  useShinyjs()
)

server <- function(input, output, session) {
  values <- reactiveValues()

  values$p <- plot_ly(
      type = 'scatter',
      mode = 'lines'
    ) %>%
      add_trace(
        y = c(rand(),rand(),rand()),
        line = list(
          color = '#25FEFD',
          width = 3
        )
      ) %>%
      add_trace(
        y = c(rand(),rand(),rand()),
        line = list(
          color = '#636EFA',
          width = 3
        )
      ) %>%
      layout(
        yaxis = list(range = c(0,10))
      )


  output$plot <- renderPlotly({values$p})

  observe({
    invalidateLater(1000, session)
    req(input$button > 0)
    plotlyProxy("plot", session) %>%
          plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(1,2))
    })
  observeEvent(input$buttonReset,{
      values$p  <- plot_ly(
        type = 'scatter',
        mode = 'lines'
      ) %>%
        add_trace(
          y = c(rand(),rand(),rand()),
          line = list(
            color = '#25FEFD',
            width = 3
          )
        ) %>%
        add_trace(
          y = c(rand(),rand(),rand()),
          line = list(
            color = '#636EFA',
            width = 3
          )
        ) %>%
        layout(
          yaxis = list(range = c(0,10))
        )
    runjs("Shiny.onInputChange('button',0)")
  })

}
shinyApp(ui, server)
库(闪亮)
图书馆(绘本)
图书馆(shinyjs)
兰特%
添加跟踪(
y=c(rand(),rand(),rand()),
行=列表(
颜色='#636EFA',
宽度=3
)
) %>%
布局(
yaxis=列表(范围=c(0,10))
)
runjs(“shinny.onInputChange('button',0)”)
})
}
shinyApp(用户界面、服务器)

希望这有帮助

非常感谢。快速问题-当extendTrace在while循环中运行时,是否有方法重置跟踪:
while(TRUE){plotlyProxy(“plot”,session)%%>%plotlyProxyInvoke(“extendTraces”,list(y=list(list(rand()),list(rand())),list(1,2))
。或者,如果我可以停止跟踪,完全删除跟踪,或者使跟踪递增,这将是理想的。基本上,如果流速率很高,浏览器会因为数据过多而开始暂停。@xbsd Hi我已使用InvalidateLater更改了代码,以进行持续的增强(不知道该单词是否存在)。还有一些shinyjs的技巧来阻止它非常好,非常感谢!了解R和无JS是一个挑战!:)
library(shiny)
library(plotly)
library(shinyjs)
rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(
  # includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly: Multiple Traces", align = "center")),
  br(),
  div(actionButton("button", "Extend Traces"),actionButton("buttonReset", "Reset Traces"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph'),
  useShinyjs()
)

server <- function(input, output, session) {
  values <- reactiveValues()

  values$p <- plot_ly(
      type = 'scatter',
      mode = 'lines'
    ) %>%
      add_trace(
        y = c(rand(),rand(),rand()),
        line = list(
          color = '#25FEFD',
          width = 3
        )
      ) %>%
      add_trace(
        y = c(rand(),rand(),rand()),
        line = list(
          color = '#636EFA',
          width = 3
        )
      ) %>%
      layout(
        yaxis = list(range = c(0,10))
      )


  output$plot <- renderPlotly({values$p})

  observe({
    invalidateLater(1000, session)
    req(input$button > 0)
    plotlyProxy("plot", session) %>%
          plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(1,2))
    })
  observeEvent(input$buttonReset,{
      values$p  <- plot_ly(
        type = 'scatter',
        mode = 'lines'
      ) %>%
        add_trace(
          y = c(rand(),rand(),rand()),
          line = list(
            color = '#25FEFD',
            width = 3
          )
        ) %>%
        add_trace(
          y = c(rand(),rand(),rand()),
          line = list(
            color = '#636EFA',
            width = 3
          )
        ) %>%
        layout(
          yaxis = list(range = c(0,10))
        )
    runjs("Shiny.onInputChange('button',0)")
  })

}
shinyApp(ui, server)