JohnCoene / echarts4r

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

e_radar not supporting group_by? #620

Open TimHirschfeld opened 4 months ago

TimHirschfeld commented 4 months ago

I am having a reactive df in an r shiny app. For ease of use, I wanted to use group_by to add multiple series to e_radar based on the reactive input, however, only the first series is displayed. e_radar appears to be the only function not supporting group_by. I build a workaround using e_list, but I was wondering if there is a simpler way?

observe({
    lapply(seq_along(input$questionnaireInput), function(index) {
      quest <- input$questionnaireInput[index]
      ns <- NS(paste0("quest", index))
      output[[ns("spider")]] <- renderEcharts4r({
        # Use intersect to get only those subvars that exist in the filtered data
        subvars <- subvariable_map[[quest]]
        existing_subvars <- intersect(names(filtered()), subvars)

        # Prepare data specifically for this plot, handling NA values
        df <- filtered() %>%
          dplyr::select(pubmed_id, dplyr::any_of(existing_subvars)) %>%
          tidyr::pivot_longer(cols = -pubmed_id, names_to = "subvariable", values_to = "effect", values_drop_na = TRUE) %>%
          dplyr::mutate(
            pubmed_id = as.factor(pubmed_id),
            effect = as.numeric(effect) # Convert effect to numeric, handling NAs and invalid conversions
          )
        # Generate indicators dynamically based on the items in the filtered data
        indicators <- lapply(unique(df$subvariable), function(item) {
          maxValue <- max(df$effect[df$subvariable == item], na.rm = TRUE)
          list(name = item, max = ceiling(maxValue * 1.1))
        })
        # Prepare series data for e_list(), ensuring to handle NA values correctly
        # Assuming 'df' is your pre-processed dataset
        series_data <- lapply(unique(df$pubmed_id), function(id) {
          # Filter data for the current pubmed_id
          df_filtered <- df %>%
            filter(pubmed_id == id) %>%
            arrange(subvariable)
          seriesValues <- df_filtered$effect

          list(
            name = as.character(id),
            type = 'radar',
            data = list(list(value = seriesValues))
          )
        })

        opts <- list(
          radar = list(indicator = indicators),
          series = series_data,
          legend = list(
            data = sapply(series_data, function(x) x$name),
            show = TRUE,
            orient = 'vertical', # Arrange legend items vertically
            right = '10', # Position the legend on the right side of the chart
            top = 'middle' # Align the legend to the middle vertically)
        )
        e_charts() %>%
          e_list(opts)
      })
...
JohnCoene commented 4 months ago

I remember when I implemented that (5+ years ago) not being sure how to handle this for the radar chart so I skipped it.

It is indeed not supported (as of yet at least)