show modal onclik plotly bar plot

plotly bar chart on click bar chart we need to open modal kindly help

Hi,

Please provide some more details and some code for us to start from. This way you will be more likely to find the help you need...

Grtz

library(shiny)
library(shinymaterial)

df1 <- data.frame(x = 1:10, y = 1:10)
df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
y = c(rnorm(10), rnorm(10, 3, 1)))

ui <- material_page(title = "Material Design",
tags$br(),
font_color = "cyan darken-5",
nav_bar_color = "cyan darken-5",
plotlyOutput('scatter')
)

server <- function(input, output) {
output$scatter <- renderPlotly({
plot_ly(df1, x = df1$x, y = df1$y, type = 'bar', source = 'scatter')
})
}

shinyApp(ui = ui, server = server)

the above mentioned code is for the bar chart in plotly, I am struck at a point where I need a help;- I want a pop-up when I click over the bar chart the respective contents to be
displayed.
Please help me on the same.

Hi,

Find below the code for a plot_ly bar chart with modal on click:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("myPlot")
)

server <- function(input, output, session) {
  
  df1 <- data.frame(x = 1:10, y = 1:10)
  df2 <- data.frame(x = c(rep('a', 10), rep('b', 10)),
                    y = c(rnorm(10), rnorm(10, 3, 1)))
  
  output$myPlot = renderPlotly({
    plot_ly(df1, x = ~x, y = ~y, type = 'bar')
  })
  
  observeEvent(event_data("plotly_click"), {
    barData = event_data("plotly_click")
    showModal(modalDialog(title = "Bar info", renderPrint(barData)))
  })
}

shinyApp(ui, server)

Unfortunately, for some reason this only works if you do not use the material_page, but stick to the regular fluidPage. I tried other shiny events like buttons clicked and they worked as expected in the material_page, but the plotly event_data("plotly_click") will not respond when material_page is used, regardless of specifying a source or not...

I don't know if this is a bug, but if no one else finds a reason for this behavior, I'll open a ticket on the package's gitHub page.

Hope this helps,
PJ

Thanks a lot for taking your time to address my concerns, the same would be very much helpful if it is available for "shinymaterial".
I am not able to get the pop for "shinymaterial", please help for the updated query.

Hi,

You're welcome.
Regarding your shiny_material implementation, I'm afraid I don't know the answer to that issue as I told you in my last post. It might be a bug, or some parameter I'm missing. If no one else here knows why this is happening, I'll open a ticket on its gitHub page.

PJ