jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
720 stars 148 forks source link

Shiny click and mouseOver events with highmaps #761

Closed mrworthington closed 1 year ago

mrworthington commented 2 years ago

Hi,

I'm trying to replicate your shiny events demo with high maps and am a bit confused as to how to return the hc-key value (which would be the county) when a click or mouseOver event occurs. Whereas the series returns a value mapped from the "group" aesthetic in outputs obtained from hchart(), the returned value is different when using highmaps. Despite looking across high maps documentation and SO, I've found no clear method for returning the data when a click or mouseOver.

In the example below, I've tried using console.log(this.name) to return the county name, but it continues to return "Series 1". I can manually change the output from series one to something else using the name aesthetic in hc_add_series(), but I can't map the values of the county name to what's returned when a click or mouseOver event occurs.

Screenshot

Screen Shot 2022-04-26 at 1 21 35 PM

Reprex

```r library(shiny) library(shinythemes) library(highcharter) library(dplyr) geojson <- download_map_data("countries/us/us-tx-all") data <- get_data_from_map(geojson) %>% select(`hc-key`) ui <- fluidPage( theme = shinytheme("paper"), tags$script(src = "https://code.highcharts.com/mapdata/countries/us/us-tx-all.js"), fluidRow( tags$hr(), column( 12, selectInput("sel", NULL, c("Preloaded map" = "preload", "Sending map" = "send")), actionButton("action", "Generate map") ), tags$hr(), column(6, highchartOutput("hcmap")), column(3, h5("MouseOver"), verbatimTextOutput("hc_1_input1")), column(3, h5("Click"), verbatimTextOutput("hc_1_input2"))) ) server <- function(input, output) { output$hcmap <- renderHighchart({ input$action data <- mutate(data, value = round(100 * runif(nrow(data)), 2)) if(input$sel == "preload") { mapdata <- JS("Highcharts.maps['countries/us/us-tx-all']") } else { mapdata <- geojson } highchart(type = "map") %>% hc_add_series( mapData = mapdata, data = data, group = "hc_key", joinBy = c("hc-key"), borderWidth = 0 ) %>% hc_chart(zoomType = "xy") %>% hc_plotOptions(series = list(dataLabels = list(enabled=FALSE), keys = "name", nullInteraction = TRUE, point = list( events = list( mouseOver = JS("function(){console.log(this.name);}") ) ), cursor = "pointer")) %>% hc_colorAxis(stops = color_stops()) %>% hc_add_event_series(event = "mouseOver") %>% hc_add_event_series(event = "click") }) output$hc_1_input1 <- renderPrint({ input$hcmap_mouseOver }) output$hc_1_input2 <- renderPrint({ input$hcmap_click }) } shinyApp(ui, server) ```

Session Info

```r R version 4.1.1 (2021-08-10) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.4 Matrix products: default LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_1.0.8 highcharter_0.9.4 shinythemes_1.2.0 shiny_1.6.0 loaded via a namespace (and not attached): [1] zoo_1.8-9 tidyselect_1.1.2 bslib_0.3.1 purrr_0.3.4 lattice_0.20-45 colorspace_2.0-3 vctrs_0.3.8 generics_0.1.2 htmltools_0.5.2 [10] yaml_2.3.5 utf8_1.2.2 rlang_1.0.2 later_1.3.0 pillar_1.7.0 jquerylib_0.1.4 glue_1.6.2 DBI_1.1.2 TTR_0.24.3 [19] lifecycle_1.0.1 quantmod_0.4.18 stringr_1.4.0 munsell_0.5.0 htmlwidgets_1.5.4 fastmap_1.1.0 httpuv_1.6.5 curl_4.3.2 fansi_1.0.3 [28] broom_0.7.12 xts_0.12.1 Rcpp_1.0.8.3 xtable_1.8-4 promises_1.2.0.1 backports_1.4.1 scales_1.1.1 cachem_1.0.6 jsonlite_1.8.0 [37] mime_0.12 digest_0.6.29 stringi_1.7.6 rlist_0.4.6.2 grid_4.1.1 cli_3.2.0 tools_4.1.1 magrittr_2.0.2 sass_0.4.1 [46] tibble_3.1.6 crayon_1.5.1 tidyr_1.2.0 pkgconfig_2.0.3 ellipsis_0.3.2 rsconnect_0.8.25 data.table_1.14.2 lubridate_1.8.0 rstudioapi_0.13 [55] assertthat_0.2.1 R6_2.5.1 igraph_1.2.11 compiler_4.1.1 ```
stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Feel free to reopen it if you find it necessary.