I want to drill down sales data from category and then to subcategory.
While doing so, in the second level graph i.e., subcategory plot, Subcategories of other category are also n X axis.
Eg: If I click furniture in the category plot, in the next plot Subcategories of other category are also visible
Screenshots attached below.
library(shiny)
library(dplyr)
library(readr)
library(data.table)
sales<-read.csv("/Users/gowthamanramadoss/Desktop/sales1.csv")
categories <- unique(sales$category)
ui <- fluidPage(
plotlyOutput("bar"),
uiOutput("back")
)
server <- function(input, output, session) {
selections <- reactiveVal()
output$bar <- renderPlotly({
nSelections <- length(selections())
print(nSelections)
if (nSelections == 0) {
sales %>%
group_by(category) %>%
summarise(pop = sum(as.numeric(sales))) %>%
plot_ly() %>%
add_bars(x = ~category, y = ~pop)
} else {
sales %>%
filter(category %in% selections()) %>%
group_by(subcategory) %>%
summarise(pop = sum(as.numeric(sales))) %>%
plot_ly() %>%
add_bars(x = ~subcategory, y = ~pop)
}
})
observeEvent(event_data("plotly_click"), {
new <- event_data("plotly_click")$x
old <- selections()
selections(c(old, new))
})
output$back <- renderUI({
if (length(selections()))
actionButton("clear", "Back", icon("chevron-left"))
})
# clear the chosen category on back button press
observeEvent(input$clear, selections(NULL))
}
Can someone help.