score:13

Accepted answer

the following code will get it done. in brief, the key steps are:

  1. when initializing the map, be sure to specify a column as its layerid. this way leaflet will know which values to return when you specify an event.
  2. create an eventreactive that returns the value of that layerid. you can then use this to subset your data as needed to pass to the chart.
  3. i like to create a reactive data frame that is the subset of the master data frame based on the clicked layerid. you could write the app without doing this but i like to separate my shiny apps out into components as much as i can.
  4. you can now use this reactive data frame - and its values - in your call to renderhighchart

hope this helps!

---
title: "flex dashboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
runtime: shiny
---

```{r setup, include=false}
library(flexdashboard)
library(shiny)
library(leaflet)
library(highcharter)

latitude<-c(35.94077, 35.83770, 35.84545, 35.81584, 35.79387, 36.05600)
longitude<-c(-78.58010, -78.78084, -78.72444, -78.62568, -78.64262,-78.67600)
amounts1<-c(27, 44, 34, 46, 25, 15)
amounts2<-c(34, 52, 35, 78, 14, 24)
ids<-c("a", "b", "c", "d", "e", "f")
df<-data.frame(ids,amounts1,amounts2,latitude,longitude)
```

column {data-width=650}
-----------------------------------------------------------------------

```{r}
output$map <- renderleaflet({

  leaflet() %>%
    addtiles() %>%
    addmarkers(data = df, lng = longitude, lat = latitude, 
               layerid = ~ids)

})

leafletoutput('map')  

```

column {data-width=350}
-----------------------------------------------------------------------

```{r}

click_marker <- eventreactive(input$map_marker_click, {

  x <- input$map_marker_click

  return(x$id)

})

data_for_chart <- reactive({

  return(df[df$ids == click_marker(), ])

})

output$chart <- renderhighchart({

  highchart() %>%
    hc_chart(type = 'column') %>%
    hc_add_series(data = c(data_for_chart()$amounts1, 
                           data_for_chart()$amounts2))

})

highchartoutput('chart')

```

Related Query

More Query from same tag