Plotly Shiny Drill Down Issue -- From Pie to Bar

I was trying to create a drill-down from pie chart to bar chart.

What I planned: with a click on the categories of the pie chart, it would return a corresponding bar chart.

I tried several times, but it did not work out with my code. However, if I switched the pie chart to a bar chart, then the drill down from bar chart to bar chart works.

I have attached a sample code for reference. Any help or advice would be much appreciated.


ui <- fluidPage(

server <- function(input, output) {
    output$category <- renderPlotly({
        sales %>%
            count(category) %>% 
            plot_ly(labels = ~category, values = ~n, type = 'pie', source = "category")
    output$sub_category <- renderPlotly({
        d <- event_data("plotly_click", source = "category")
        if (is.null(d)) return(NULL)
        sales %>%
            filter(category %in% d$x) %>%
            count(sub_category) %>%
            plot_ly(x = ~sub_category, y = ~n, color = ~sub_category, source = "category", type = 'bar')

shinyApp(ui, server)