Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e_radar not supporting group_by? #620

Open
TimHirschfeld opened this issue Mar 4, 2024 · 1 comment
Open

e_radar not supporting group_by? #620

TimHirschfeld opened this issue Mar 4, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@TimHirschfeld
Copy link

TimHirschfeld commented Mar 4, 2024

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 JohnCoene added the enhancement New feature or request label Mar 5, 2024
@JohnCoene
Copy link
Owner

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants