'Highcharter - Clickable Pie Chart - How to get the category name from the slice clicked on a Pie Chart in Shiny?

I'm trying to click on a category in a pie chart built with highcharts and use the category to filter data in a line chart in R shiny app.



Solution 1:[1]

You can capture the click using the hc_plotOptions settings, like so:

library(shiny)
library(highcharter)

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

server <- function(input, output){

    click_js <- JS("function(event) {Shiny.onInputChange('pieclick',event.point.name);}")

    output$hcontainer <- renderHighchart({
        highchart() %>% 
            hc_chart(type = "pie") %>% 
            hc_add_series(data = list(
                list(y = 3, name = "cat 1"),
                list(y = 4, name = "dog 11"),
                list(y = 6, name = "cow 55"))) %>% 
            hc_plotOptions(series = list(events = list(click = click_js)))
    })

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

}

shinyApp(ui, server)

enter image description here

Solution 2:[2]

Following the previous answer from @porkChop you can also add below code to your hc_plotOptions in order to get a selection visualization.

hc_plotOptions(
series = list(
stacking = FALSE, allowPointSelect = TRUE ,events = list(click = click_js))
)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pork Chop
Solution 2 Yasin Amini