Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Section on restoring state from a bookmark? #45

Open
cpsievert opened this issue Jan 23, 2019 · 1 comment
Open

Section on restoring state from a bookmark? #45

cpsievert opened this issue Jan 23, 2019 · 1 comment

Comments

@cpsievert
Copy link
Owner

Sort of like this example, which currently requires plotly/plotly.R#1392

library(shiny)
library(plotly)

ui <- function(req) {
  fluidPage(
    bookmarkButton(),
    plotlyOutput("plot")
  )
}

server <- function(input, output, session) {
  
  output$plot <- renderPlotly({
    plot_ly(economics, x = ~pop, source = "pop_hist")
  })
  
  onRestored(function(state) {
    hist_relayout <- jsonlite::parse_json(
      state$input[["plotly_relayout-pop_hist-input"]]
    )
    xrng <- as.numeric(hist_relayout[c("xaxis.range[0]", "xaxis.range[1]")])
    
    if (length(xrng)) {
      plotlyProxy("plot") %>% 
        plotlyProxyInvoke("relayout", list(xaxis = list(range = xrng)))
    }
  })
}

shinyApp(ui, server, enableBookmarking = "url")
@cpsievert
Copy link
Owner Author

cpsievert commented Jan 23, 2019

It's worth noting the example above could maybe handled automatically by plotly someday (seems like any restyle/relayout could be handled), so it's probably not a good idea to put in the book, but it might be worth demonstrating bookmarking with a 'side-effecty' example like persistent brushing with accumulating data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant