Comments (2)
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.
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)
- `expression(list())` introduced when using labeller in facet
- Support for geom_lolli and geom_flame from heatwaveR in plotly
- When calling on geom_area tooltip activates when hovering over line, not area
- Legend name not passed when using frame animation
- Capital Letters affect alphabetical ordering causing wrong order of legend and x axis
- Bubble sizes out of order, when using a formula for the `color`/`name` attributes HOT 2
- render ggplotly graphic - unable to start device HOT 1
- Could subplot have explicit error when ncols is passed?
- `ggplotly` not taking full vertical space when inside `shiny::tabPanel()` HOT 1
- Large mesh3d plot is not displayed with R-4.4.0 HOT 2
- Plots not showing in Rstudio Viewer HOT 6
- Categorical ggplotly legend no longer interpreted as categorical when two aesthetics are used HOT 1
- Change default plot height from 450px to something user-selected.
- Missing legend for mesh3d unless surface is added
- ggplotly is not working properly with scale_fill_binned
- `ggplotly()` gives subscript error with latest ggplot2
- Update plotly.js? HOT 1
- Incomplete documentation for `plot_ly`
- eom_GeomArcBar() has yet to be implemented in plotly
- plotly cannot handle Inf or -Inf when plotting
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from plotly.r.