Lilou Ptk
Lilou Ptk

Reputation: 1

Access value stored in a JavaScript Variable with Shiny.onInputChange (rCharts) when clicking on highchart graph

I need to access the value of one of my parameters by clicking on the point representing it in a RShiny graph.

For that I used the Javascript function Shiny.onInputChange, but I can't get the value of the variable country... I tried this.name, event.point.name, event.country, this.values, event.item.values, this.country, event.point.category but none of them works...

Here is a transcript of my code:

library(shiny)
library(highcharter)
library(gapminder)


ui <- fluidPage(
  column(12, highchartOutput("hcontainer",height = "300px")),
  column(12, textOutput("clicked")))

server <- function(input, output){
  
  click_js <- JS("function(event) {Shiny.onInputChange('scatterclick', event.point.name);}")
  
  output$hcontainer <- renderHighchart({  
    
    gapminder::gapminder %>%
      
      hchart(type = "scatter", 
             hcaes(x = lifeExp, y = gdpPercap, group = continent), showInLegend = TRUE)  %>%
      
      hc_plotOptions(series = list(stacking = FALSE, events = list(click = click_js)))
    
  })
  
  output$clicked <- renderText({input$scatterclick  })
  
}

shinyApp(ui, server)

Upvotes: 0

Views: 119

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84519

When you don't know, do:

click_js <- JS("function(event) {console.log(event);}")

Then run the app, click, and look at the console of the inspector tools. That's how I got the solution:

  click_js <- JS(
    "function(event) {Shiny.setInputValue('scatterclick', event.point.country);}"
  )

Upvotes: 0

Related Questions