如何在 Shiny 应用程序中通过 echarts4r 使用回调

问题描述

我希望在服务器的 echarts4r 对象中检索用户选择的信息,用于服务器端逻辑。我希望使用 https://echarts4r.john-coene.com/reference/echarts4r-shiny.html 处的回调 id_clicked_dataid_mouSEOver_data 等。

在玩具应用程序(下方)中,我希望返回地图上点击的国家/地区的值,以通过 outpt_map_country 对象将该值返回到屏幕。任何帮助将不胜感激谢谢。

我尝试过 input$outpt_map_country_clicked_data 的变体,但解决方案是在暗指我。

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)

解决方法

您尝试使用的回调是 id_clicked_data,但您提供的是打印输出的 ID,而不是地图输出。

input$outpt_map_country_clicked_data 替换为 input$outpt_map_clicked_data 就可以了:

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)