简体   繁体   中英

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? , I am trying to capture the output of a click event on a highcharter treemap in an R Shiny app.

I think I just need the right name to replace "event.point.name" in this javascript function:

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. but with the plot options: Changing it to this:

 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)

代表输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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