JohnCoene / echarts4r

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

Dynamically update axes when changing the chart type via proxies #573

Open j-kreis opened 9 months ago

j-kreis commented 9 months ago

I would like to transition between chart types dynamically (#440). Could you please provide details on how to use e_morph within a shiny app using a proxy?

library(echarts4r)
library(shiny)

ui <- fluidPage(
  actionButton("switch", "Switch data"),
  echarts4rOutput("plot")
)

server <- function(input, output, session) {

  data = tibble::tibble(chr = sample(letters[1:5], size = 100, replace = TRUE),
                        num = rnorm(100))

  column = reactiveVal("chr")

  output$plot <- renderEcharts4r({
    echarts4r::e_charts(data) |>
    echarts4r::e_axis_labels(x = "", y = "")
  })

  observeEvent(input$switch, {

    col = setdiff(colnames(data), column())

    active_class = class(data[[col]])

    echarts4rProxy(id = "plot", session = session) |>
      e_remove_serie_p(serie_index = 0)|>
      e_remove_serie_p(serie_index = 1)

    if (active_class =="numeric") {
      echarts4rProxy(id = "plot", session = session,
                     data = data)|>
        e_density(num) |>
        e_execute()
    } else {
      data |>
        dplyr::ungroup() |>
        dplyr::count(!!rlang::sym(col)) |>
        echarts4rProxy(id = "plot", session = session) |>
        echarts4r::e_bar(n, legend = FALSE) |>
        e_execute()
    }

    column(col)
  })
}

shinyApp(ui, server)
munoztd0 commented 9 months ago

Not really sure what do you want, maybe screenshots of what you expect would help ? related to #570 ?

j-kreis commented 9 months ago

I wanted to generically switch between plot types. Similar to this example. Switching between bar, scatter, pie and other plots, using the same or different data.

JohnCoene commented 9 months ago

Then look at what @munoztd0 shared, you want e_morph, see docs let us know if you have questions on how to implement it