Using Proxy Interface in Plotly/Shiny to dynamically change data

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)

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 ?

Thank you for any answer.

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)

addTraces with mode=text and axis rescaling not compatible with , I can successfully add traces, but if I switch animation on, the added text /using-​proxy-interface-in-plotly-shiny-to-dynamically-change-data if  I always use proxy for updating plotly (and DT). The main challenge is that plotlyProxyInvoke takes plotly arguments structured in the list structure of the underlying javascript, which was slightly modified in R-plotly. Basically you need to pass all arguments as nested lists.

The restyle API is a bit wonky...I forget the reasoning, but data arrays like x and y need double arrays. I'd do it this way:

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)

Proxy Interface | R, Currently, you must use the plotlyProxyInvoke() function to modify a plotlyProxy() object, which requires knowledge/use of a plotly.js method for the updating logic (​  I'm taking a look first at how it works with a Shiny app. Webjars - I can see that the main template of shinyproxy loads the bootstrap CSS which also gets utilised by each Shiny app. Load from the container - Additional static content for shiny apps is requested by using the container name, in this case peaceful_jepsen e.g.:

library(shiny)

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

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

    output$graphe <- renderPlotly({
        plot_ly() %>%
            layout(title="test") %>%
            add_trace(x=runif(2), y=runif(2), name="ABC_test", type="scatter", mode="lines+markers")
    })

    observeEvent(input$update, {
        plotlyProxy("graphe", session, FALSE) %>%
            plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
            plotlyProxyInvoke("addTraces", list(x=runif(2),
                                                y=runif(2),
                                                name="ABC_test",
                                                type = 'scatter',
                                                mode = 'lines+markers'))
    })

}

shinyApp(ui, server)

17 Server-side linking with shiny, Most plotly.js events are accessible through this interface – for a complete list see FIGURE 17.7: Editing circle shape positions to dynamically alter a linear model To trigger a modification of a plotly output widget, you must create a proxy  That SO question is really about Plotly.restyle(), but I get your point It's important to know that when you use plotlyProxy(), you're working directly with plotly.js, and plotly.js is more strict about values than plot_ly() and ggplotly(). For example if you look at the data (i.e., traces) component behind:

plotly 4.7.1 now on CRAN, In other words, this proxy interface allows one to perform more efficient and responsive updates to a plotly graph within a shiny app. Cpsievert's interactive graph and data is a contour. which requires knowledge/use of a plotly.js method for the updating logic (among them, Plotly.restyle, Plotly.relayout,  It is not uncommon that the user interface of a Shiny application needs to be generated dynamically, based on data or program state. One typical use case that we encounter frequently is when the UI lets the user edit a variable number of records from a database.

Choose a RData dataset before launching rest of Shiny app, Choose a RData dataset before launching rest of Shiny app · shiny Using Proxy Interface in Plotly/Shiny to dynamically change data. r, shiny  The dynamic proxy that will be generated works by implementing all the interfaces of a given type. The dynamic proxy will also maintain a reference to the invocation handler that the user defined. For every method declared in the type’s interface(s), a simple implementation is generated that makes a call to the proxy handler’s Invoke method.

Leveraging htmlwidget shiny proxies in rmarkdown for responsive , Have been following along with Carson Sievert's RStudio webinar Leveraging htmlwidget shiny proxies in rmarkdown for responsive updating Accessing and responding to Plotly events in Shiny - Carson Sievert An important aspect in the design of these proxy interfaces is that you need to know the  Stack Overflow | The World’s Largest Online Community for Developers

Comments
  • Thanks but it doesn't update the data of the current trace, but add another trace
  • @JulienVernay, you can a line to delete the previous point. See edits.
  • It should be list(as.integer(0)) because plotly traces are zero indexed.
  • I added the addTraces bug report on github. Wow I spent several days struggling with this bug.