Pascal Schmidt
Pascal Schmidt

Reputation: 243

How to use e_remove_serie_p to remove series individually without re-drawing others?

I would like to remove each continent individually when removing a series with the checkboxGroupInput. In the documentation I only saw how to do it when the continents would be column names. However, I have one column with different levels where each level is a continent. I don't know how I could proceed. If possible, I would like to solve it with the e_remove_serie_p function instead of some custom JavaScript.

library(shiny)
library(echarts4r)
library(dplyr)
library(gapminder)
# Prepare the summarized data
gapminder_summary <- gapminder %>%
  group_by(continent, year) %>%
  summarize(mean_lifeExp = mean(lifeExp), .groups = 'drop')
# Define UI for the application
ui <- fluidPage(
  # Application title
  titlePanel("Mean Life Expectancy by Continent Over Time"),
  # Sidebar layout with input and output definitions
  sidebarLayout(
    sidebarPanel(
      checkboxGroupInput(
        inputId = "selected_continents",
        label = "Select Continents:",
        choices = unique(gapminder_summary$continent),
        selected = unique(gapminder_summary$continent)
      )
    ),
    # Main panel for displaying outputs
    mainPanel(
      echarts4rOutput(outputId = "lifeExpPlot"),
      br(),
      br()
    )
  )
)
# Define server logic required to draw the plot
server <- function(input, output) {
  # Reactive expression to filter data based on user input
  filtered_data <- reactive({
    gapminder_summary %>%
      filter(
        continent %in% input$selected_continents
      )
  })
  output$lifeExpPlot <- renderEcharts4r({
    req(nrow(filtered_data()) > 0)
    filtered_data() %>%
      group_by(continent) %>%
      e_chart(x = year) %>%
      e_line(mean_lifeExp) %>%
      e_y_axis(
        min = min(filtered_data()$mean_lifeExp),
        max = max(filtered_data()$mean_lifeExp)
      ) %>%
      e_x_axis(
        min = min(filtered_data()$year),
        max = max(filtered_data()$year)
      )
  })
  observeEvent(input$selected_continents, {
    # some code to remove series
    echarts4rProxy("lifeExpPlot") %>%
      e_remove_serie_p(serie_index = 1)
  })
}
# Run the application
shinyApp(ui = ui, server = server)

Upvotes: 0

Views: 38

Answers (0)

Related Questions