5

I have the following problem and I hope you can help me:

I use plotly, shiny and RStudio and I want to achieve the following:

  • to have one line plot with some data
  • to dynamically generate histogram of other values while pointing mouse on some point on the first plot

I've managed to do so and it is working, but it is working extremely slow! Point somewhere, wait 3 seconds, build histogram, point somewhere else, wait again, generate histogram, and so on. And it only has the very basic features so far. Is there any chance it can be more smooth and quick? Should I try to do it in Python or some other language?

Below is the code. You can download sample data from https://github.com/ymra/dynamic_histogram/blob/master/dji.csv. Also, you have to know that the histogram is generated for 650 observations (starting from the mouse point and going back). The first 650 observations (from 0 to 650) are NA.

library(shiny)
library(plotly)
library(moments)

ui <- fluidPage(
  h2("Prices and return rate"),
  plotlyOutput("plot"),
  h2("Histogram"),
  h3("move mouse over line line plot"),
  plotlyOutput("plot2")
)

server <- function(input, output) {

  # Read and transform data
  alltogether <- data.frame(read.csv(file = "./dji.csv", sep = ",", header = TRUE))

  #change column names to simplify
  colnames(alltogether) <- c("date","returns","prices")

  # Generate statistic magic values
  for (i in 3000:650){
    n = i-649
    alltogether$means[i] <- mean(alltogether$prices[n:i])
  }

  # render 
  output$plot <- renderPlotly({ 

    ay <- list(
      tickfont = list(color = "red"),
      overlaying = "y",
      side = "right",
      range = c(-0.14,0.3)
    )
    p <- plot_ly() %>%
      add_lines(data = alltogether, x = ~date, y = ~prices, name = "price", text = ~paste("mean: ", alltogether$means)) %>%
      add_lines(data = alltogether, x = ~date, y = ~returns, name = "return", yaxis = "y2") %>%
      layout(title = "Price and return rate", yaxis2 = ay)
  })


  output$plot2 <- renderPlotly({
    d <- event_data("plotly_hover")
    b <- d$pointNumber[1]
    e <- b+649
    rangy <- (alltogether$returns[b:e])
    if (b > 649){ 
      plot_ly(x=rangy,type = "histogram")
    }
  })

}
ymra
  • 51
  • 2
  • 1
    Could you provide a reproducible exemple.. http://stackoverflow.com/help/how-to-ask and http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – timat Apr 13 '17 at 11:17
  • Sure, I have updated the post. – ymra Apr 13 '17 at 13:02
  • 1
    Same problem for me. For similar 3d scatter plots with plotly scatter3d and an rgl scatter3d plot the rendering times were 15-20 sec vs 2-3 – Mark Jun 01 '17 at 15:53
  • 1
    good to know not only me is strugging with this ;) – ymra Jun 02 '17 at 13:08
  • The same problem with me for rgl after upgrading R3.2->R3.4. https://stackoverflow.com/questions/47950439/rgl-became-too-slow-after-upgrading-r Going back R3.2 – Viktor Dec 23 '17 at 12:41
  • Same problem for me. I'll let you guys know if I figure anything out. – Joe Flack Sep 24 '20 at 20:33

0 Answers0