首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新数据后,r亮高图保留点击点的颜色。

更新数据后,r亮高图保留点击点的颜色。
EN

Stack Overflow用户
提问于 2022-09-15 19:17:22
回答 1查看 72关注 0票数 0

我建立了一个R闪亮+高图表散点图,其中的数据更新跨年动画。在下面的虚拟版本中,您可以看到两个点在启动动画时从左到右徘徊。

由于真实的情节包含更多的点,我希望用户能够点击感兴趣的点,然后改变他们的颜色,这样用户就可以更容易地跟踪变化。

我设法在点击这些点时将它们的颜色更改为红色。但是,在启动动画时,由于数据不断更新,单击点再次失去颜色。

我想知道如何在每次更新中保留着色。

代码语言:javascript
复制
library(shiny)
library(highcharter)
library(dplyr)

dat <- 
  data.frame(
    year=seq(2000,2020),
    x=seq(0,20),
    y=seq(0,20),
    name=letters[seq(1,21)]
  ) %>% 
  add_row(
    data.frame(
      year=seq(2000,2020),
      x=seq(2,22),
      y=seq(2,22),
      name=letters[seq(1,21)]
    ) )

shinyApp(
  
  ui=fluidPage(
    
    # Slider ------------------------------------------------------------------------------------- #
    sliderInput(
      inputId = "g2_slider",
      label = NULL,
      min = 2000,
      max = 2020,
      value = 2000,
      step = 1,
      sep = "",
      animate = animationOptions(
        interval = 1000,
        loop = FALSE,
        playButton = actionButton("play", "Play", icon = icon("play"), width = "100px", style = "margin-top: 10px"),
        pauseButton = actionButton("pause", "Pause", icon = icon("pause"), width = "100px", style = "margin-top: 10px")
      )),
    
    actionButton("reset", "Reset", width = "100px", style = "margin-top: -87px"),
    
    # Scatterplot -------------------------------------------------------------------------------- #
    
    highchartOutput("g2_plot"),
    
  ), 
  
  server=function(input, output, session) {
    
    x_min <- dat %>% pull(x) %>% min()
    x_max <- dat %>% pull(x) %>% max()
    y_min <- dat %>% pull(y) %>% min()
    y_max <- dat %>% pull(y) %>% max()
    
    # Reset button ------------------------------------------------------------------------------- #
    
    observeEvent(input$reset, {
      updateSliderInput(
        session = session,
        inputId = "g2_slider",
        value = 2000
      )
    })
    
    # Scatterplot -------------------------------------------------------------------------------- #
    
    output$g2_plot <- renderHighchart({ 
      
      highchart() %>% 
        hc_add_series(
          data = dat %>% filter(year==input$g2_slider),
          type = "scatter",
          animation=FALSE,
          mapping = 
            hcaes(
              x=x,
              y=y,
              name=name
            ),
          id="scatter1"
        ) %>% 
        hc_plotOptions(
          series = list(
            point = list(
              events = list(
                click = JS("function(event) { this.update({color: 'red'}) }") 
              )))) %>% 
        hc_title(text = paste0("Year: ", input$g2_slider)) %>% 
        hc_xAxis(
          title = list(text = "x"),
          min=x_min, 
          max=x_max
        ) %>%
        hc_yAxis(
          title = list(text = "y"),
          min=y_min,
          max=y_max
        ) %>%
        hc_legend(enabled = FALSE) 
    })
    
    # Update scatterplot ------------------------------------------------------------------------- #
    
    observeEvent(input$g2_slider, {
      
      highchartProxy("g2_plot") %>%
        hcpxy_update_series(
          id = "scatter1",
          data = dat %>% filter(year==input$g2_slider),
          mapping = hcaes(
            x=x,
            y=y,
            name=name
          )) %>% 
        hcpxy_update(title = list(text = paste0("Year: ", input$g2_slider)))
      
    })
    
  })
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-16 15:58:26

@magdalena解决了这个问题。通过给每个数据点一个id,在更新中保留颜色。

代码语言:javascript
复制
dat <- 
  data.frame(
    year=seq(2000,2020),
    id=1,
    x=seq(0,20),
    y=seq(0,20),
    name=letters[seq(1,21)]
  ) %>% 
  add_row(
    data.frame(
      year=seq(2000,2020),
      id=2,
      x=seq(2,22),
      y=seq(2,22),
      name=letters[seq(1,21)]
    ) )


shinyApp(
  
  ui=fluidPage(
    
    # Slider -------------------------------- #
    sliderInput(
      inputId = "g2_slider",
      label = NULL,
      min = 2000,
      max = 2020,
      value = 2000,
      step = 1,
      sep = "",
      animate = animationOptions(
        interval = 1000,
        loop = FALSE,
        playButton = actionButton("play", "Play", icon = icon("play"), width = "100px", style = "margin-top: 10px"),
        pauseButton = actionButton("pause", "Pause", icon = icon("pause"), width = "100px", style = "margin-top: 10px")
      )),
    
    actionButton("reset", "Reset", width = "100px", style = "margin-top: -87px"),
    
    # Scatterplot ---------------------------- #
    
    highchartOutput("g2_plot"),
    
  ), 
  
  server=function(input, output, session) {
    
    x_min <- dat %>% pull(x) %>% min()
    x_max <- dat %>% pull(x) %>% max()
    y_min <- dat %>% pull(y) %>% min()
    y_max <- dat %>% pull(y) %>% max()
    
    # Reset button --------------------------- #
    
    observeEvent(input$reset, {
      updateSliderInput(
        session = session,
        inputId = "g2_slider",
        value = 2000
      )
    })
    
    # Scatterplot ---------------------------- #
    
    output$g2_plot <- renderHighchart({ 
      
      isolate({
        
        highchart() %>% 
          hc_add_series(
            data = dat %>% filter(year==input$g2_slider),
            type = "scatter",
            animation=FALSE,
            mapping = 
              hcaes(
                id=id,
                x=x,
                y=y
              ),
            id="scatter1"
          ) %>% 
          hc_plotOptions(
            series = list(
              type="scatter", 
              data=dat %>% filter(year==input$g2_slider),
              point = list(
                events = list(
                  click = JS("function(event) { this.update({color: 'red'}) }") 
                )))) %>% 
          hc_title(text = paste0("Year: ", input$g2_slider)) %>% 
          hc_xAxis(
            title = list(text = "x"),
            min=x_min, 
            max=x_max
          ) %>%
          hc_yAxis(
            title = list(text = "y"),
            min=y_min,
            max=y_max
          ) %>%
          hc_legend(enabled = FALSE) 
        
      })
      
    })
    
    # Update scatterplot --------------------- #
    
    observeEvent(input$g2_slider, {
      
      highchartProxy("g2_plot") %>%
        hcpxy_set_data(
          type = "scatter",
          data = dat %>% filter(year==input$g2_slider),
          mapping = 
            hcaes(
              id=id,
              x=x,
              y=y
            )
        ) %>% 
        hcpxy_update(title = list(text = paste0("Year: ", input$g2_slider)))
      
    })
    
  })
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73736333

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档