Thanks so much, Nir! i could use the click value to filter the boxplot.
Realised that for pie, the click returns the value based on the height and not the piece of pie that is clicked.
So i guess i will have to go with the bar chart after all. That works too !
Pasting the updated code here. But noticed that the boxplot do not load at all on initial page render now. The box plot appears only after the bar is clicked. Any thoughts on that?
library(ggplot2)
library(dplyr)
library(shiny)
library(shinyWidgets)
library(shinydashboard)
ui <- fluidPage(
useShinydashboard(),
titlePanel("My Pie Chart"),
mainPanel(
fluidRow(
box(
plotOutput(outputId = "piePlot", click = "plot_click"),
verbatimTextOutput("result")
#verbatimTextOutput("mouse")
),
box(
plotOutput(outputId = "boxPlot")
)
),
fluidRow(
box(
plotOutput(outputId = "barPlot", click = "bplot_click"),
verbatimTextOutput("barresult")
#verbatimTextOutput("mouse")
),
box(
plotOutput(outputId = "boxPlot1")
)
)
)
)
# Define server logic required to draw pie chart and boxplot
server <- function(input, output) {
bp_data <- mtcars %>%
group_by(gear) %>%
summarise(count = n()) %>%
mutate(per=round(100*count/sum(count),1)) %>%
ungroup ()
bp_decode <- bp_data %>% arrange((gear)) %>%
mutate(csum=cumsum(per),
lagcsum=lag(csum,default=0))
################
###Pie Chart####
################
output$piePlot <- renderPlot({
bp <- bp_data %>%
ggplot(aes(x = "", y = per, fill = gear, label = per)) +
geom_bar(width = 1, stat = "identity") +
geom_col() +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
ylab("Percent") +
xlab("")
piePlot <- bp + coord_polar("y", start = 0)
return(piePlot)
})
click_result <- reactive({
ipc <- req(input$plot_click)
y_val <- ipc$y
bp_decode %>% rowwise %>% filter(
between(y_val,
lagcsum,
csum)) %>% pull(gear)
})
output$result <- renderPrint({
req(click_result())
})
#display the return on pie click below the pie
output$mouse <- renderPrint({
text <- str(input$plot_click)
return(text)
})
#Boxplot corresponding to pie chart
output$boxPlot <- renderPlot({
mtcars$gear = as.factor(mtcars$gear)
box <- ggplot(mtcars, aes(x = gear, y = mpg), color = gear)+geom_boxplot()
if (!is.null(req(click_result()))){
mtcars_new <- mtcars %>%
filter(gear == req(click_result()))
box <- ggplot(mtcars_new, aes(x = gear, y = mpg), color = gear)+geom_boxplot()
}
return(box)
})
################
###Bar Chart####
################
output$barPlot <- renderPlot({
bp <- bp_data %>%
ggplot(aes(x = "", y = per, fill = gear, label = per)) +
geom_bar(width = 1, stat = "identity") +
geom_col() +
geom_text(size = 3, position = position_stack(vjust = 0.5)) +
ylab("Percent") +
xlab("")
return(bp)
})
# Display the boxplot1 corresponding to bar chart
output$boxPlot1 <- renderPlot({
mtcars$gear = as.factor(mtcars$gear)
box <- ggplot(mtcars, aes(x = gear, y = mpg), color = gear)+geom_boxplot()
if (!is.null(req(bclick_result()))){
mtcars_new <- mtcars %>%
filter(gear == req(bclick_result()))
box <- ggplot(mtcars_new, aes(x = gear, y = mpg), color = gear)+geom_boxplot()
}
return(box)
})
bclick_result <- reactive({
ipc <- req(input$bplot_click)
y_val <- ipc$y
bp_decode %>% rowwise %>% filter(
between(y_val,
lagcsum,
csum)) %>% pull(gear)
})
output$barresult <- renderPrint({
req(bclick_result())
})
}
# Run the application
shinyApp(ui = ui, server = server)