hsilva hsilva - 2 months ago 88
R Question

How to open a bsmodal shiny R

Clicking on a button is the only way to create a bsmodal window?

Is it possible, for instance, to click on a highchart bar and open a bsmodal window?

Thank you in advance

Answer

In highchars (and then highcharter) you need to use javascript events. You can know when the user click some series in a chart. In particular you can use something like this using the answer given by @Skalbhile using jquery and the modal's name:

    highchart() %>% 
      hc_chart(type = "column") %>% 
      hc_add_series(data = c(1, 2, 3)) %>%
      hc_add_series(data = c(2, 1, 3), name = "other data") %>% 
      hc_plotOptions(
        series = list(
          point = list(
            events = list(
              click = JS("function(){
                         /* alert(this.series.name + ' ' + this.category); */
                         /* here you activate trigger the modal */
                         $('#modalExample').modal('show');
                         }")
              )
            )
          )
        )

So finally a demo can be:

library(shiny)
library(shinyBS)
library(highcharter)

shinyApp(
  ui =
   fluidPage(
  highchartOutput("chart"),
  bsModal("modalExample", "Data Table", "tabBut", size = "large",
          "Modal Content")
),
server =
function(input, output, session) {

  output$chart <- renderHighchart({

    highchart() %>% 
      hc_chart(type = "column") %>% 
      hc_add_series(data = c(1, 2, 3)) %>%
      hc_add_series(data = c(2, 1, 3), name = "other data") %>% 
      hc_plotOptions(
        series = list(
          point = list(
            events = list(
              click = JS("function(){
                         /* alert(this.series.name + ' ' + this.category); */
                         /* here you activate trigger the modal */
                         $('#modalExample').modal('show');
                         }")
              )
            )
          )
        )
    })


})