在 Plotly/Shiny 中使用代理接口动态更改数据

Jul*_*nay 5 r shiny r-plotly

我想使用Proxy Interface更新图中存在的数据(显示在 Shiny 应用程序中的 plotlyOutput 中)。这是一个最小的 App.R 代码:

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)
Run Code Online (Sandbox Code Playgroud)

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

谢谢你的任何回答。

Car*_*son 7

APIrestyle有点奇怪......我忘记了推理,但是数据数组就像xy需要双数组。我会这样做:

library(shiny)
library(plotly)

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

server <- function(input, output, session) {
  output$graphe <- renderPlotly({
    plot_ly() %>%
      add_markers(x = 0, y = 0, name = "ABC_test") %>%
      layout(title = "test")
  })

  observeEvent(input$update, {
    plotlyProxy("graphe", session) %>%
      plotlyProxyInvoke("restyle", "y", list(list(1)), 0)
  })
}

shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)


MLa*_*oie 6

奇怪的是,addTraces它不仅仅适用于一点,而是适用于两点。为了使它起作用,您可以将同一点添加两次。所以你可以试试这个:

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)
Run Code Online (Sandbox Code Playgroud)