I have two plotly plots in a shiny dashboard. When I click on the first plotly plot, the interactive event is working fine. But when I perform the same operation on the second plot which is a stacked barplot, the window is closing automatically.
Do you have come across the shiny dashboards with more than one plotly plots? If yes, how to handle the click events on different plotly plots?
I am preparing the reproducible usecase. Soon I will post it.
library(shinydashboard)
library(plotly)
library(shiny)
library(dplyr)
library(ggplot2)
tg <- ToothGrowth
tg$dose <- factor(tg$dose)
skin <- Sys.getenv("DASHBOARD_SKIN")
skin <- tolower(skin)
if (skin == "")
skin <- "blue"
sidebar <- dashboardSidebar(
sidebarSearchForm(label = "Search...", "searchText", "searchButton"),
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))
)
)
body <- dashboardBody(
tabItems(
tabItem("dashboard",
fluidRow(
box(
title = "Distribution",
status = "primary",
plotlyOutput("plot1", height = "auto"),
height = 500,
width = 7
),
box(
height = 500, width = 5,
title = "Dist",
plotlyOutput("click", height = 430)
)
)
)
))
header <- dashboardHeader(
title = "My Dashboard"
)
ui <- dashboardPage(header, sidebar, body, skin = skin)
server <- function(input, output, session) {
output$plot1 <- renderPlotly({
p <- ggplot(data = tg, aes(x=len, y=dose, col=supp, key=supp)) + geom_point()
ggplotly(p)
})
output$click <- renderPlotly({
d <- event_data("plotly_click")
if (is.null(d)){
"Click events appear here (double-click to clear)"
} else {
gSel <- tg %>% filter(dose %in% d$y) %>% group_by(supp) %>% mutate(newLen=floor(len)) %>%
ggplot(aes(x=supp, fill=as.factor(newLen))) + geom_bar()
ggplotly(gSel)
}
})
}
shinyApp(ui, server)
How to avoid the available error in the above image? Text printing in the plot output area.
The first plot is used for iterative click events. When I click a point on y=1
, it produces the second plot
But When I click on the stacked bar, the second plot becomes missing
(In my original scenario, the window closes and not visible. To use the app, I need to rerun the app).
How to receive the click events and check if it is from first plot or second plot?