简体   繁体   English

highcharter treemap 中的点击事件(R shiny)

[英]Click event in highcharter treemap (R shiny)

Based on the answer to this question: Highcharter - Clickable Pie Chart - How to get the category name from the slice clicked on a Pie Chart in Shiny?基于这个问题的答案: Highcharter - Clickable Pie Chart - How to get the category name from the slice clicked on a Pie Chart in Shiny? , I am trying to capture the output of a click event on a highcharter treemap in an R Shiny app. ,我正在尝试在 R Shiny 应用程序中的 highcharter treemap 上捕获点击事件的 output。

I think I just need the right name to replace "event.point.name" in this javascript function:我想我只需要正确的名称来替换这个 javascript function 中的“event.point.name”:

function(event) {Shiny.onInputChange('treemapclick', event.point.name);} 

but I am not sure where to look.但我不确定在哪里看。

Thanks in advance for your generosity!提前感谢您的慷慨!

library(shiny)
library(highcharter)
library(gapminder)


ui <- fluidPage(
  column(12, highchartOutput("hcontainer",height = "300px")),
  column(12, textOutput("clicked")))

server <- function(input, output){
  
  click_js <- JS("function(event) {Shiny.onInputChange('treemapclick', event.point.name);}")
  
  output$hcontainer <- renderHighchart({  
      
        gapminder::gapminder %>%
        filter(year  == 2007) %>% 
        highcharter::data_to_hierarchical(group_vars = c(continent, country), size_var = pop) %>% 
        hchart(type = "treemap") %>% 
        hc_plotOptions(events = list(click = click_js))


  })
  
  output$clicked <- renderText({
    input$treemapclick
  })
  
}

shinyApp(ui, server)

树形图点击事件

Figured out the problem.想通了这个问题。 The issue was not with the event.point,name variable.问题不在于 event.point,name 变量。 but with the plot options: Changing it to this:但使用 plot 选项:将其更改为:

 hc_plotOptions(treemap = list(events = list(click = click_js)))

does the trick.做的伎俩。

Working reprex:工作代表:

library(shiny)
library(highcharter)
library(gapminder)


ui <- fluidPage(
  column(12, highchartOutput("hcontainer",height = "300px")),
  column(12, textOutput("clicked")))

server <- function(input, output){
  
  click_js <- JS("function(event) {Shiny.onInputChange('treemapclick', event.point.name);}")
  
  output$hcontainer <- renderHighchart({  
      
        gapminder::gapminder %>%
        filter(year  == 2007) %>% 
        highcharter::data_to_hierarchical(group_vars = c(continent, country), size_var = pop) %>% 
        hchart(type = "treemap") %>% 
        hc_plotOptions(treemap = list(events = list(click = click_js)))

  })
  
  output$clicked <- renderText({input$treemapclick  })
  
}

shinyApp(ui, server)

代表输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM