Accepted answer

i'll probably go more into detail this time about what you can do and what possibilities you have:

a highchart event function always has the form js("function(event) { ... }"). the js stands for javascript and prevents r from interpreting the code inside. luckily, javascript looks a lot like r, which will make it easier to write statements even if you don't know any javascript.

the function inside always has to have an event parameter. from the highcharts documentation, this event has several properties, most important of all, point. properties in javascript can be accessed by a dot ., just like you would use a $ in r. so event.point will be the point you clicked on.

now, this point also has a lot of properties, of which most are not very useful. but possible values you might want to use are category, color, x and y for respective axis values. so if you'd like to know the y value of the clicked point, event.point.y is the command. another example: in your case, event.point.x would yield the x value, which in the case of a bar chart is not the category, but the index of the category (counting starts at 0 here). in the previous example (your code snippet), i also used the object this. this stands for the series that has been clicked. this is just a shortcut, because you could also get the series via event.point.series. and the series itself has a lot of properties, like i.e. the name.

note concerning javascript: lists (arrays) in javascript are created with the [ ... ] brackets. i used this in the code below to send multiple values in one command.

that is the first part. now, you want to communicate the selection to your shiny app. shiny has inbuilt information channels for this purpose. one is the javascript function shiny.oninputchange. this function has two arguements, the second one is the value you want to send. the first one is the name, under which you want to access the value later on. so the usage is always shiny.oninputchange('myvar', value). now this value gets sent to your server and is accessible under input$myvar. there, you have the same rules as for any other input. you can put input$myvar into reactive environments and they will react, whenever something new is sent to input$myvar.

concerning your direct request: to know what category the clicked value belongs to, can be achieved by changing the initial click function just a bit. (i renamed the functions to suit their functionality.) the explanation above should make it understandable. the second feature, with the legend click, uses another event "slot" that is built into highcharts. the legenditemclick works just like the normal click, but only listens to legend clicks. there are some more event handlers that can be assigned, i.e. mouseover if you need hover events.

okay, now here is a sample code to give a working example for what i wrote above:

best regards


ui <- shinyui(
    column(width = 8, highchartoutput("hcontainer", height = "500px")),
    column(width = 4, textoutput("text"))

server <- function(input, output) {      

  a <- data.frame(b = letters[1:10], c = 11:20, d = 21:30, e = 31:40)

  output$hcontainer <- renderhighchart({      

    canvasclickfunction <- js("function(event) {shiny.oninputchange('canvasclicked', [, event.point.category]);}")
    legendclickfunction <- js("function(event) {shiny.oninputchange('legendclicked',;}")

    highchart() %>% 
      hc_xaxis(categories = a$b) %>% 
      hc_add_series(name = "c", data = a$c) %>%
      hc_add_series(name = "d", data = a$d) %>% 
      hc_add_series(name = "e", data = a$e) %>%
      hc_plotoptions(series = list(stacking = false, events = list(click = canvasclickfunction, legenditemclick = legendclickfunction))) %>%
      hc_chart(type = "column")



  observeevent(input$canvasclicked, {
    outputtext <<- paste0("you clicked on series ", input$canvasclicked[1], " and the bar you clicked was from category ", input$canvasclicked[2], ".") 

  observeevent(input$legendclicked, {
    outputtext <<- paste0("you clicked into the legend and selected series ", input$legendclicked, ".")

  output$text <- rendertext({

shinyapp(ui, server) 

Related Query

More Query from same tag