I have a barplot in R shiny. I want following:
When user clicks one "bar" in that barplot another box will pop up and show the data information from the used datadframe showing which data points contributed to create that "bar".
Code:
ui <- fluidPage(
sidebarLayout(sidebarPanel(),
mainPanel(plotOutput("p",click = "plot_click"),
textOutput("info"))))
server <- function(input,output) {
output$p <- renderPlot(ggplot(mtcars,aes(x=factor(carb),y=mpg))+
geom_bar(stat="summary",fun.y="mean"))
output$info <- renderText(
paste0("x=", input$plot_click$x, "\n",
"y=", input$plot_click$y))
}
shinyApp(ui, server)
When I'm clicking on a bar, it is showing the (x,y) co-ordinate value. But I want to retrieve the corresponding factor(carb). How to get back source information back if I click on a bar. The ideal case would be: When user clicks on a bar which has carb=4, it should show the source information of the mtcars dataset with carb=4. But I'm stuck how to get the "carb=4" information interactively from that bar plot.