JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
601 stars 81 forks source link

e_scatter: error index out of scope #370

Open Camil88 opened 3 years ago

Camil88 commented 3 years ago

Hello, I created e_scatter chart which is dynamically changing based on user choice in dropdown input. The issue is that when I 'deselect all' choices in dropdown list then chart disappears from screen and 'index out of scope' error comes up (on a screen and in R console). It's interesting that when I run the app for the first time then I can see the same error in R console, however chart renders on a screen. When I do the same with e_bar then everything's ok, the problem is with e_scatter. Here is reproducible example:

library(shiny)
library(dplyr)
library(echarts4r)
library(nycflights13)
library(shinyWidgets)

moduleServer <- function(id, module) {
    callModule(module, id)
}

# UI #
mod_btn_UI <- function(id) {

    ns <- NS(id)
    tagList(
        uiOutput(ns("list")),
        echarts4rOutput(ns("plot"))
    )
}

# Server #
mod_btn_server <- function(id){
    moduleServer(id, function(input, output, session) {

      ns <- NS(id)

      df <- nycflights13::weather[c(1:10,9000:9010,18000:18010),]

      data <- reactive({
        df %>% 
          dplyr::select(origin) %>% 
          unique()
      })

      airports <- reactive({
        subset(df, df$origin %in% input$picker)
      }) 

      output$list <- renderUI({
        ns <- session$ns

        shinyWidgets::pickerInput(
          inputId = ns('picker'),
          label = NULL,
          multiple = TRUE,
          options = list(`actions-box` = TRUE, title = "Choose airport"),
          choices = data()$origin,
          selected = data()$origin
        )
      })

      output$plot <- renderEcharts4r({
        airports() %>%
          e_charts(temp) %>%
          e_scatter(dewp, humid, bind = origin) %>% 
          e_tooltip(
            trigger = "item",
            formatter = htmlwidgets::JS("
              function(params){
                return('<strong>' + params.name +
                        '</strong><br />temp: ' + params.value[0] +
                        '<br />dewp: ' + params.value[1] +
                        '<br />humid: ' + params.value[2])
                        }
            "))
      })
    })
}

# App #
ui <- fluidPage(
    mod_btn_UI("test-btn")
)
server <- function(input, output, session) {
    mod_btn_server("test-btn")
}
shinyApp(ui = ui, server = server)
JohnCoene commented 3 years ago

Sorry. not sure what is happening here either. I don't think it's a bug though. I'll try to investigate further.

Camil88 commented 3 years ago

I've noticed that grouping by column added further as bind solves the problem. I binded by origin and needed to group by origin as well. Then the chart does not disappear:

      output$plot <- renderEcharts4r({
        airports() %>%
          group_by(origin) %>%
          e_charts(temp) %>%
          e_scatter(dewp, humid, bind = origin) %>% 
JohnCoene commented 3 years ago

Ah thanks for this and sorry for the trouble, I'll try to understand what happens exactly!