CallumH
CallumH

Reputation: 779

How to use Callbacks with echarts4r in a Shiny App

I am looking to retrieve the user selected information in echarts4r objects in the server, for server side logic. I am looking to use the callbacks id_clicked_data, id_mouseover_data, etc found here https://echarts4r.john-coene.com/reference/echarts4r-shiny.html.

In the toy app (below) I am looking to return the value of the clicked country on the map to return the value to the screen via the outpt_map_country object. Any help would be greatly appreciated thanks.

I have tried with variations of input$outpt_map_country_clicked_data but the solutions is alluding me.

library(shiny)
library(echarts4r)
library(countrycode)
library(dplyr)


ui <- fluidPage(
  echarts4rOutput('outpt_map'),
  verbatimTextOutput('outpt_map_country'),
  tableOutput('out_cns')
)


cns <- data.frame(
  country = countrycode::codelist$country.name.en
) %>% 
  mutate(value   = round(runif(length(country), 1, 5), 6))

server <- function(input, output, session) {
  
  
  output$outpt_map <- renderEcharts4r({
    
    cns %>% 
      e_charts(country) %>% 
      e_map(value) 
  })
  
  output$outpt_map_country <- renderPrint({
    
    input$outpt_map_country_clicked_data
    
  })
  
  output$out_cns <- renderTable({
    
    cns
    
  })
  
}

shinyApp(ui, server)

Upvotes: 1

Views: 323

Answers (1)

bretauv
bretauv

Reputation: 8567

The callback you are trying to use is id_clicked_data, but you give the id of the print output instead of the map output.

Replace input$outpt_map_country_clicked_data by input$outpt_map_clicked_data and it works:

library(shiny)
library(echarts4r)
library(countrycode)
library(dplyr)


ui <- fluidPage(
  echarts4rOutput('outpt_map'),
  verbatimTextOutput('outpt_map_country'),
  tableOutput('out_cns')
)


cns <- data.frame(
  country = countrycode::codelist$country.name.en
) %>% 
  mutate(value   = round(runif(length(country), 1, 5), 6))

server <- function(input, output, session) {
  
  
  output$outpt_map <- renderEcharts4r({
    
    cns %>% 
      e_charts(country) %>% 
      e_map(value) 
  })
  
  output$outpt_map_country <- renderPrint({
    
    input$outpt_map_clicked_data
    
  })
  
  output$out_cns <- renderTable({
    
    cns
    
  })
  
}

shinyApp(ui, server)

Upvotes: 1

Related Questions