Как получить потоковую гистограмму в R? - PullRequest
0 голосов
/ 09 октября 2019

Потоковые графики (также известные как графики в реальном времени) довольно легко использовать в виде графиков, как показано в этом уроке, может ли потоковый граф быть выполнимым для гистограммы.

Я адаптировал данный пример, но не смог заставить его работать. Вот воспроизводимый код Shiny, просто скопируйте его в файл app.R и запустите его в Rstudio:

app.R

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(

  headerPanel(h1("Streaming in Plotly", align = "center")),
  br(),
  div(actionButton("button", "Extend Trace"), align = "center"),
  br(),
  div(plotlyOutput("plot2"), id='graph')
)

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

  p2 <- plot_ly(
    x = rnorm(n=1),
    type = 'histogram')%>%
    layout(
      xaxis = list(range = c(-10,10)),
      yaxis = list(range = c(0,10))
    )
  ##
  ## Output to UI
  output$plot2 <- renderPlotly(p2)
  ##
  ## Update once button clicked
  observeEvent(input$button, {
    data <- rnorm(n=1000)
    #while(TRUE){
    for( i in 1:1000 ){
      val <- data[i]
      Sys.sleep(0.1)
      plotlyProxy("plot2", session) %>%
        plotlyProxyInvoke("extendTraces", list(y=list(list(val))), list(0))
    }
  })
}

shinyApp(ui, server) 

1 Ответ

1 голос
/ 09 октября 2019

Вам нужно будет изменить x значения в plotlyProxyInvoke, а не y:

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(

  headerPanel(h1("Streaming in Plotly", align = "center")),
  br(),
  div(actionButton("button", "Extend Trace"), align = "center"),
  br(),
  div(plotlyOutput("plot2"), id='graph')
)

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

  p2 <- plot_ly(
    x = rnorm(n=1),
    type = 'histogram')%>%
    layout(
      xaxis = list(range = c(-10,10)),
      yaxis = list(range = c(0,10))
    )
  ##
  ## Output to UI
  output$plot2 <- renderPlotly(p2)
  ##
  ## Update once button clicked
  observeEvent(input$button, {
    data <- rnorm(n=1000)
    #while(TRUE){
    for( i in 1:1000 ){
      val <- data[i]
      Sys.sleep(0.1)
      plotlyProxy("plot2", session) %>%
        plotlyProxyInvoke("extendTraces", list(x=list(list(val))), list(0))
    }
  })
}

shinyApp(ui, server) 
...