Giter VIP home page Giter VIP logo

Comments (2)

dvg-p4 avatar dvg-p4 commented on August 23, 2024

Hmm, looking a bit further into the code, this might be a misunderstanding on my part of what exactly "plotly_relayout" represents. If I'm starting to understand correctly, it seems to fire when the javascript Plotly.relayout() function is called, returning the values it was called with. So, it's more "event-like" and less "state-like" than I thought it was supposed to be. Still, I don't see any other way to get the current x-range and y-range of the plot, so some way to do this or workaround or clarification would be appreciated.

from plotly.r.

dvg-p4 avatar dvg-p4 commented on August 23, 2024

To answer my own question, here's an example of how to use relayout to keep track of x-bounds robustly:

library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("myplot"),
  selectInput("color_choice",
    label = "Choose a new plot color to redraw the plot:",
    choices = c("red", "green", "blue")
  ),
  div("plotly_relayout:"),
  verbatimTextOutput("relayout"),
  div("xbounds:"),
  verbatimTextOutput("xbounds_out"),
  div("Data currently within plot bounds:"),
  verbatimTextOutput("table")
)

server <- function(input, output, session) {
  xbounds <- reactiveVal(NULL) # NULL indicates autorange

  output$myplot <- renderPlotly({
    p <- plot_ly(faithful,
      type = "scatter",
      mode = "markers",
      x=~eruptions,
      y=~waiting,
      color = I(input$color_choice)
    ) %>% rangeslider()
    xbounds(NULL) # The plot resets to autorange when redrawn
    return(p)
  })

  observe({
    d <- event_data("plotly_relayout")
    if (isTRUE(d$xaxis.autorange)){
      xbounds(NULL)
    } else if (isTruthy(d$xaxis.range)) { # As triggered by the rangeslider
      xbounds(c(d$xaxis.range[1], d$xaxis.range[2]))
    } else if (isTruthy(d[["xaxis.range[0]"]])) { # As triggered by the plot
      xbounds(c(d[["xaxis.range[0]"]], d[["xaxis.range[1]"]]))
    }
    # otherwise, do NOT update the bounds, since relayout will also be
    # triggered by e.g. dragmode/hovermode updates or the window resizing
  })

  output$relayout <- renderPrint({
    return(event_data("plotly_relayout"))
  })

  output$xbounds_out <- renderPrint({
    return(xbounds())
  })

  output$table <- renderPrint({
    data <- faithful %>% dplyr::arrange(eruptions)
    bounds <- xbounds()
    if (is.null(bounds)) {
      data
    } else {
      data %>% dplyr::filter(dplyr::between(eruptions, bounds[1], bounds[2]))
    }
  })
}

shinyApp(ui = ui, server = server)

Given that the reactiveVal() solution addresses my actual problem much more robustly than an implementation of my initial request here would, I'll close this ticket.

from plotly.r.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.