在绘制数千点时提高rCharts的性能

时间:2015-01-14 10:45:30

标签: r highcharts shiny rcharts

我正在尝试使用rCharts包绘制数千个点,但我发现默认参数有点慢。我试图将turboThresold参数设置为0,但它没有帮助。

这是一张表演表:

  • 密谋100分:00.05秒
  • 密谋1000分:00.54秒
  • 密谋10000点:17.00秒

任何人都可以提高此代码的性能吗?

library(shiny)
library(rCharts)
runApp(
  # User interface with 1 select box, 1 graph and 1 timer
  list(ui = pageWithSidebar(  
    headerPanel(""),  
    sidebarPanel( 
      selectInput("value",NULL,choices=c(100,1000,10000), selected = 100)
    ),
    mainPanel(       
      showOutput("plot", "highcharts"),
      textOutput("timer")
    )   
  ), 
  # server side
  server = function(input, output){
    values<-reactiveValues()
    values$timer <- NULL
    # generating highcharts plot
    output$plot <- renderChart2({
      x <- 1:input$value
      df <- data.frame(x, x^2)
      names(df) <- c("x","xpower2")
      values$timer <- Sys.time()
      plot <- Highcharts$new()
      plot$series(
        data = toJSONArray2(df, json = F, names = F),
        name = "xpower2",
        type = "line"
      )
      plot$plotOptions(series=list(turboThreshold=0))
      return(plot)
    })
    # calculating time 
    output$timer <- renderText({
      input$value
      isolate({
        values$timer <- Sys.time() - values$timer
        return(paste("Time elapsed :", round(values$timer,3) , "seconds"))
      })
    })
  }
  )
)

感谢您的帮助, 马特

1 个答案:

答案 0 :(得分:0)

通常,我建议使用任何浏览器图形包向浏览器发送不超过3000点的信息。选项是无穷无尽的,但取决于数据。除此之外,我无法使用rCharts加快速度,但这里只有ggvis

library(shiny);library(ggvis)
runApp(
  list(ui = fluidPage(
  titlePanel('ggvis speed example'),  
  sidebarLayout(  
    sidebarPanel( 
      selectInput('points','Points To Plot',
                  choices=c(100,1000,10000,100000), selected = 1000)
    ),
    mainPanel(       
      ggvisOutput('ggvis')
    )   
  )), 
  server = function(input, output){
    reactive({
        1:input$points %>%             
        data.frame('x'=.,'y'=.^2) %>%
        ggvis(~x, ~y) %>% 
        layer_points() %>%
        add_axis("y", title_offset = 70) %>%
        add_tooltip(function(df) HTML(paste0('x: ',df$x,'<br>y: ',df$y)))
    }) %>% bind_shiny('ggvis')
  }
  )
)