使用R HIghcharter Library单击鼠标后添加和附加系列

时间:2017-03-14 14:51:43

标签: r shiny r-highcharter

单击鼠标后,我需要能够在绘图中添加另一条迹线。我正在使用R的Web框架Shiny在Web浏览器中显示绘图。我想要添加的系列是点或任何系列。

我还需要在情节上画线。我想点击一个起点和一个结尾,一条线穿过点击的点。

这是我到目前为止所做的。

#############To Update
#if (!require("devtools"))
#install.packages("devtools")
#devtools::install_github("jbkunst/highcharter")

library("shiny")
library("highcharter")

dots<-hc_add_series_scatter(cars$speed, cars$dist)

hc_base <- highchart() %>% 
  hc_xAxis(categories = citytemp$month) %>% 
  hc_add_series(name = "Tokyo", data = citytemp$tokyo) 

ui <- fluidPage(
  h2("Viewer"),
  fluidRow(
    h3(""), highchartOutput("hc_1", width = "100%", height = "800px"),
     h3("Click"), verbatimTextOutput("hc_1_input2")
  )
)
server = function(input, output) {
  output$hc_1 <- renderHighchart({
       hc_base %>% 
          hc_add_theme(hc_theme_ffx())%>%
                 hc_tooltip(backgroundColor="skyblue",crosshairs = TRUE, borderWidth = 5, valueDecimals=2)%>%
                      hc_add_event_series(series="dots", event = "click")
})
output$hc_1_input2 <- renderPrint({input$hc_1_click })
}
shinyApp(ui = ui, server = server)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

这可能是这样做的一种方式:

library(shiny)
library(highcharter)


hc_base <- highchart() %>% 
  hc_xAxis(categories = citytemp$month) %>% 
  hc_add_series(name = "Tokyo", data = citytemp$tokyo) 

ui <- fluidPage(
  h2("Viewer"),
  fluidRow(
    h3(""), highchartOutput("hc_1", width = "100%", height = "800px"),
    h3("Click"), verbatimTextOutput("hc_1_input2")
  )
)
server = function(input, output) {
  output$hc_1 <- renderHighchart({
    hc_base %>% 
      hc_add_theme(hc_theme_ffx())%>%
      hc_tooltip(backgroundColor="skyblue",crosshairs = TRUE, borderWidth = 5, valueDecimals=2)%>%
      hc_add_event_point(event = "click")
  })

  observeEvent(input$hc_1_click,{
    output$hc_1 <- renderHighchart({
      hc_base %>% 
        hc_add_theme(hc_theme_ffx())%>%
        hc_tooltip(backgroundColor="skyblue",crosshairs = TRUE, borderWidth = 5, valueDecimals=2)%>%
        hc_add_series_scatter(cars$speed, cars$dist)
    })

  })

  output$hc_1_input2 <- renderPrint({input$hc_1_click })
}
shinyApp(ui = ui, server = server)

希望它有所帮助!