【问题标题】:Use return data from dynamic highcharts chart in Shiny在 Shiny 中使用来自动态 highcharts 图表的返回数据
【发布时间】:2016-02-12 22:00:59
【问题描述】:

我正在 Shiny 中实现一个图表,用户可以使用 http://jkunst.com/highcharter/oldindex.html#draggable-points 示例中的“可拖动点”选项创建自己的时间序列。我想使用 server.r 中的“新”数据点并将它们显示在表格中。如果有人可以帮助我如何返回数据点,那就太好了。请在下面找到代码。

谢谢,蒂姆

server.r:

data(citytemp, package = "highcharter")
   function(input, output) {
 hcbase <- reactive({
   # hcbase <- function() highchart() 
   hc <- highchart() 


   if (input$credits)
     hc <- hc %>% hc_credits(enabled = TRUE, text = "Highcharter", href =    "http://jkunst.com/highcharter/")

if (input$exporting)
  hc <- hc %>% hc_exporting(enabled = TRUE)

if (input$theme != FALSE) {
  theme <- switch(input$theme,
                  null = hc_theme_null(),
                  economist = hc_theme_economist(),
                  dotabuff = hc_theme_db(),
                  darkunica = hc_theme_darkunica(),
                  gridlight = hc_theme_gridlight(),
                  sandsignika = hc_theme_sandsignika(),
                  fivethirtyeight = hc_theme_538(),
                  chalk = hc_theme_chalk(),
                  handdrwran = hc_theme_handdrawn()
    )

    hc <- hc %>% hc_add_theme(theme)
  }

  hc

})


 output$table <-renderDataTable({
   #Output from graph
   data.table(month=citytemp$month,berlin=citytemp$berlin
              ,berlin_dragged=citytemp$berlin)#Here I want to use the dragged data. something linke input$highchart$... should do the trick I guess...
 })

output$highchart <- renderHighchart({

data(citytemp)
highchart() %>% 
  hc_chart(animation = FALSE) %>% 
  hc_title(text = "draggable points demo") %>% 
  hc_xAxis(categories = month.abb) %>% 
  hc_plotOptions(
    series = list(

    stickyTracking = FALSE
        ),
    column = list(
      stacking = "normal"
    ),
    line = list(
      cursor = "ns-resize"
    )
    ) %>% 

  hc_add_series(
    data = citytemp$berlin,
    draggableY = TRUE
  )

})



 }
ui.r

library("shiny")
library("shinydashboard")
library("highcharter")
library("dplyr")
library("viridisLite")
library("markdown")
library("quantmod")
library("tidyr")
library("ggplot2")
library("treemap")
library("forecast")
library("DT")
rm(list = ls())

dashboardPage(
  skin = "black",
  dashboardHeader(title = "highcharter", disable = FALSE),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Examples", tabName = "examples", icon = icon("bar-chart"))
    )
    #,
    #div(includeMarkdown("hcterinfo.md"), style = "padding:10px")
  ),
  dashboardBody(
    # tags$head(tags$script(src = "js/ga.js")),
    # tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "css/custom_fixs.css")),
    tabItems(
      tabItem(tabName = "examples",
              fluidRow(

              box(width = 6, highchartOutput("highchart")),
              box(width = 6, dataTableOutput("table"))

      )
    )
  )
))

【问题讨论】:

标签: r highcharts shiny


【解决方案1】:

关键部分是:

  • 在 highcharts 中使用 drop 事件(JS 代码)。
  • 在前面的代码事件中使用 Shiny.onInputChange('inputname', data)。这告诉 Shiny 手动预测数据已更改,因此您可以在渲染函数中使用。
  • 然后创建一个调用 input$inputname 的 renderObject (renderTable)。这样您就可以获取更新后的数据并为所欲为。

关键代码行

    hc_plotOptions(
    series = list(
      point = list(
        events = list(
          drop = JS("function(){
                    console.log(this.series)
                    window.data = _.map(this.series.data, function(e) { return e.y })
                    Shiny.onInputChange('inputname', data);
                    }"))
          )))

然后:

renderDataTable({
   ...
   var <- input$inputname # listening the drop event 
   ...

参考:https://github.com/jbkunst/shiny-apps/blob/master/highcharter/server.R#L158

【讨论】:

  • 我真的很喜欢这个库,但是对于大型数据集(>100k 点),渲染速度有点慢,所以我坚持使用dygraphs。是否会实施其他功能来支持大型数据集?
  • @PorkChop Right hc 不太好用。 Highcharts 团队正在开发boost 模块。我试图 implement 这个模块但破坏了其他功能(也许我会单独实现这个 htmwidget/module)。 dygraphs 也是一个不错的图表库,遗憾的是仅面向 ts。
  • 是的 Boost 很好,我记得有人向Rcharts 提出过这个建议,Ramnath 说他会调查的。无论如何,图书馆做得很好!
猜你喜欢
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-25
  • 1970-01-01
相关资源
最近更新 更多