dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

Interactive callback values #7

Closed systats closed 4 years ago

systats commented 4 years ago

Hi great idea to port apexchart.js! I switched all my stuff from highcharts to apex, thanks for that. I wonder whether it is possible to get a shiny input value when the plot is clicked? I did not find anything on that subject. Best, Simon

pvictor commented 4 years ago

Hello Simon, thanks for the kind words and the support ! Yes there's not much documentation on how to interact with Shiny, I will write a vignette when I figure out how to make it easy to be used from R. For the moment you have an example on how to use a JS function as callback in ?events_opts, all JS callbacks can be defined here.

Here an imperfect example, maybe there's a better way to retrieve x-axis info, i'll look into it :

library(shiny)
library(apexcharter)

ui <- fluidPage(
  fluidRow(
    column(
      width = 8, offset = 2,
      tags$h2("Apexchart in Shiny"),
      apexchartOutput("chart"),
      verbatimTextOutput(outputId = "res_click")
    )
  )
)

server <- function(input, output, session) {

  output$chart <- renderApexchart({
    apexchart() %>%
      ax_chart(
        type = "bar",
        events = events_opts(
          dataPointSelection = JS(
            "function(event, chartContext, config) {
                console.log(config);
               Shiny.setInputValue('click', {
                raw: config.selectedDataPoints,
                category: config.w.config.xaxis.categories[config.selectedDataPoints]
               })
              }"
          ) 
        )
      ) %>%
      ax_series(
        list(
          name = "Example",
          data = sample(1:100, 5)
        )
      ) %>%
      ax_xaxis(
        categories = LETTERS[1:5]
      )
  })

  output$res_click <- renderPrint({
    input$click
  })
}

shinyApp(ui, server)

Victor

systats commented 4 years ago

Thanks a lot!

pvictor commented 4 years ago

I added new functions to interact with charts in shiny, usage is described here : https://dreamrs.github.io/apexcharter/articles/shiny-integration.html And it's now on CRAN!