在 Plotly/Shiny 中使用代理接口动态更改数据 [英] Using Proxy Interface in Plotly/Shiny to dynamically change data

查看:51
本文介绍了在 Plotly/Shiny 中使用代理接口动态更改数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 代理接口.这是一个最小的 App.R 代码:

I want to update the data present in a plot (displayed in plotlyOutput in a Shiny app) using Proxy Interface. Here is a minimal App.R code :

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) %>%
            plotlyProxyInvoke("restyle", list(x=0,y=1),0)
    })
}

shinyApp(ui, server)

当我运行它时,绘图显示在 (0,0) 处(根据需要),但是当我单击测试"按钮时,该点不会移动到 (0,1).我怎样才能做到这一点?

When I run it, the plot is displayed with a dot at (0,0) (as wanted) but when I click of the button "Test", the dot does not move to (0,1). How can I achieve this ?

感谢您的回答.

推荐答案

奇怪的是 addTraces 不能只处理一点,而是处理两点.为了使其工作,您可以将同一点添加两次.所以你可以试试这个:

Strangely enough addTracesdoes not work with only one point but works with two points. To make it work you could add the same point twice. So you could try this:

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, {
    plotlyProxy("graphe", session) %>%
    plotlyProxyInvoke("deleteTraces", list(as.integer(1))) %>%
    plotlyProxyInvoke("addTraces", list(x=c(0, 0),y=c(1, 1),
                        type = 'scatter',
                        mode = 'markers'))
  })
}

shinyApp(ui, server)

这篇关于在 Plotly/Shiny 中使用代理接口动态更改数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆