5

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.

3 Answers 3

Reset to default

Trending sort

Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.

It falls back to sorting by highest score if no posts are trending.

6

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)
4
  • Thanks but it doesn't update the data of the current trace, but add another trace Jun 10, 2018 at 19:17
  • @JulienVernay, you can a line to delete the previous point. See edits.
    – MLavoie
    Jun 10, 2018 at 23:52
  • It should be list(as.integer(0)) because plotly traces are zero indexed. Jun 16, 2019 at 23:33
  • I added the addTraces bug report on github. Wow I spent several days struggling with this bug. Jul 12, 2019 at 20:12
3

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)
1
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.