You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Plotly/Shiny中用Proxy Interface动态更新图表数据?

解决PlotlyProxy更新Scatter点位置无效的问题

你的问题出在plotlyProxyInvoke调用restyle方法时的参数格式上。Plotly的restyle API要求,当更新类似x、y这类数组型属性时,需要传递数组形式的值,而不是单个数值。

修正后的完整代码

library(shiny)
library(plotly)

ui <- fluidPage(
  actionButton("update", "Test"),
  plotlyOutput("graphe")
)

server <- function(input, output, session) {
  output$graphe <- renderPlotly({
    p <- plot_ly(type="scatter", mode="markers")
    p <- layout(p, title="test")
    p <- add_trace(p, x=0, y=0, name="ABC_test", mode="lines+markers")
  })
  
  observeEvent(input$update, {
    proxy <- plotlyProxy("graphe", session) %>%
      # 将x和y的值包裹为数组(R中用list实现)
      plotlyProxyInvoke("restyle", list(x=list(0), y=list(1)), 0)
  })
}

shinyApp(ui, server)

关键修正说明

  • 原代码中list(x=0,y=1)是传递单个数值,Plotly无法正确解析为trace的坐标数组;
  • 修改为list(x=list(0), y=list(1))后,相当于告诉Plotly:把索引为0的trace的x坐标数组更新为[0],y坐标数组更新为[1];
  • 最后一个参数0指定了要更新的trace索引(从0开始计数,这里只有一个trace,所以用0)。

运行修正后的代码,点击"Test"按钮后,点就会成功移动到(0,1)的位置了。

内容的提问来源于stack exchange,提问作者Julien Vernay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:36:40