echarts 4 r与tabsetPanel -重新动画切换回选项卡时

brccelvz  于 2023-05-04  发布在  Echarts
关注(0)|答案(1)|浏览(221)

我希望图表显示动画每次你点击一个标签,而不仅仅是第一次。这是我得到的

library(echarts4r)
library(magrittr)

ui <- fluidPage(
  tabsetPanel(id = "tabsetpanel", 
              tabPanel("Panel A", value = "A", echarts4rOutput("chartA")
              ),
              tabPanel("Panel B", value = "B", echarts4rOutput("chartB")
              )
  )
)

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

  output$chartA <- renderEcharts4r({
    data <- data.frame(x = 1:2, y = 1:10)
    
    input$tabsetpanel
    
    e_charts(data, x = x) %>%
    e_bar(serie = y)
  })
  
  output$chartB <- renderEcharts4r({
    data <- data.frame(x = 1:5, y = 11:20)
    
    input$tabsetpanel
    
    e_charts(data, x = x) %>%
      e_bar(serie = y)
  })
    
}

shinyApp(ui, server)

包括输入$tabsetpanel非常接近我想要的,它使表达式无效。然而,在一瞬间,图表似乎缩小了,只有在那时它才重新开始。这看起来有点恶心,有更好的方法吗?

7eumitmz

7eumitmz1#

我们可以添加一个观察者,它对活动选项卡中的更改做出React,并重新呈现相应的图表。这可确保每次用户切换到不同选项卡时播放动画:

library(echarts4r)
library(magrittr)
library(shiny)

ui <- fluidPage(
  tabsetPanel(id = "tabsetpanel", 
              tabPanel("Panel A", value = "A", echarts4rOutput("chartA")
              ),
              tabPanel("Panel B", value = "B", echarts4rOutput("chartB")
              )
  )
)

server <- function(input, output, session) {
  
  current_tab <- reactiveVal("A")
  
  observeEvent(input$tabsetpanel, {
    current_tab(input$tabsetpanel)
  })
  
  output$chartA <- renderEcharts4r({
    data <- data.frame(x = 1:2, y = 1:10)
    
    e_charts(data, x = x) %>%
      e_bar(serie = y)
  })
  
  output$chartB <- renderEcharts4r({
    data <- data.frame(x = 1:5, y = 11:20)
    
    e_charts(data, x = x) %>%
      e_bar(serie = y)
  })
  
  observe({
    if (current_tab() == "A") {
      output$chartA <- renderEcharts4r({
        data <- data.frame(x = 1:2, y = 1:10)
        
        e_charts(data, x = x) %>%
          e_bar(serie = y)
      })
    } else {
      output$chartB <- renderEcharts4r({
        data <- data.frame(x = 1:5, y = 11:20)
        
        e_charts(data, x = x) %>%
          e_bar(serie = y)
      })
    }
  })
}

shinyApp(ui, server)

相关问题