Hi,
Your example was still not a reprex, as it required me to copy paste and edit the plain text data you provided and then create a data frame from it after which I needed to replace all the database code. However, I could see you put a lot of effort in trying to come up with one so I went through the extra trouble of cleaning it all up 
The data you provide is not enough to recreate the carts you want, but I think I can show you what the functionality is you need:
library(RODBC)
library(RODBCext)
library(plotly)
library(shinydashboard)
library(shiny.semantic)
library(shiny)
library(dplyr)
dtd = data.frame(
id = c(101L, 201L, 301L, 401L, 501L, 601L, 701L, 801L, 901L, 102L),
totalraj = c(12456L, 32564L, 32564L, 78564L, 96547L, 86542L, 45632L,
45261L, 98562L, 12478L),
totalother = c(65452L, 25456L, 32542L, 23564L, 25463L, 78965L, 45632L,
45236L, 12546L, 32564L),
districtname = as.factor(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")),
tehid = as.factor(c("null", "null", "null", "null", "null", "null",
"null", "null", "null", "null")),
blockname = as.factor(c("null", "null", "null", "null", "null", "null",
"null", "null", "null", "null")),
cid = as.factor(c("null", "null", "null", "null", "null", "null",
"null", "null", "null", "null")),
centername = as.factor(c("null", "null", "null", "null", "null", "null",
"null", "null", "null", "null"))
)
ui <- fluidPage(
plotlyOutput("boxplot1"),
plotlyOutput("boxplot2"),
plotlyOutput("boxplot3")
)
server <-function(input, output, session) {
output$boxplot1 <- renderPlotly({
ds <- data.frame(labels = c("totalraj","totalother"),
values = c(dtd$totalraj,dtd$totalother)
)
plot_ly(ds, labels = ~labels, values = ~values,type = 'pie', source = "pieChart")%>%
layout(legend = list(orientation = "h",
xanchor = "left"))
})
output$boxplot2 <- renderPlotly({
s <- event_data("plotly_click", source = "pieChart")
req(!is.null(s))
tablename=rowSums(cbind(dtd$totalraj,dtd$totalother),na.rm=TRUE)
plotData = dtd %>% mutate(allSums = totalraj + totalother)
layout <- list(
font = list(size = 12),
title = "",
xaxis = list(title = ""),
yaxis = list(title = " ",automargin = TRUE)
)
p <- plot_ly(data = plotData, x = ~districtname, y = ~allSums, name = "", type = 'bar', source = "barChart")
p <- layout(p, font = layout$font, title = layout$title, xaxis = layout$xaxis, yaxis = layout$yaxis)
p
})
output$boxplot3 <- renderPlotly({
s <- event_data("plotly_click", source = "barChart")
req(!is.null(s))
plotData = dtd %>% filter(districtname == s$x)
p <- plot_ly(data = plotData, x = c("totalraj", "totalother"),
y = c(~totalraj, ~totalother), name = "", type = 'bar')
p
})
}
shinyApp(ui,server)
- When the app starts, you'll only see the pie chart
- When you click on the pie, the bar chart with all districts and their sum appears (doesnt matter where on the chart you click, as it won't influence the next chart according to you instructions)
- When you click a bar in the district barchart, you'll get the details in a next chart (is dependent now on which bar you click). You did not provide me info/data on what should be in that chart, so I made something up.
- Note that i use
req() function to make sure a chart will only be drawn once its trigger has been clicked.
Hope this helps
PJ
PS: notice how the data now is pasted into my code for immediate use. Look at the datapasta package and the reprex guide to know how to do this in future!