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

Issue restoring from bookmark URL #1378

Closed
jrowen opened this issue Sep 19, 2016 · 3 comments
Closed

Issue restoring from bookmark URL #1378

jrowen opened this issue Sep 19, 2016 · 3 comments
Assignees

Comments

@jrowen
Copy link

@jrowen jrowen commented Sep 19, 2016

The new bookmarking functionality is great. I'm attempting to use it with rhandsontable and running into a slight issue when attempting to restore the table data. The widget is currently build as a shiny output and Shiny.onInputChange is used to create a pseudo input.

The bookmark logic is encoding the onInputChange data returned from the widget and making that data available in in the onRestore method. I added a simple variable cache to stop this input and then use it to rebuild the table when the app is loaded. This is working very well except for the data parameter, which is a JS array passed to R from the browser app.

I think I've traced this back to a difference between how the data is passed to R from onInputChange and how the data is restored from the bookmark URL.

Below is an example app and a "fix" using jsonlite. Is this the expected shiny behavior, and if so should we look to update rhandsontable to to get around this issue (maybe using JSON.stringify)?

library(shiny)
library(rhandsontable)

ui <- function(request) {
  fluidPage(
    titlePanel("Handsontable"),
    sidebarLayout(
      sidebarPanel(
        helpText("Handsontable demo output. Column add/delete does work ",
                 "for tables with defined column properties, including type."),
        radioButtons("useType", "Use Data Types", c("TRUE", "FALSE")),
        bookmarkButton()
      ),
      mainPanel(
        rHandsontableOutput("hot", width = 350)
      )
    )
  )
}

server <- function(input, output, session) {
  cache_tbl = NULL

  onRestore(function(state) {
    tmp = state$input$hot
    tmp$data = jsonlite::fromJSON(
      jsonlite::toJSON(tmp$data), simplifyVector = FALSE)
    cache_tbl <<- tmp
  })

  data = reactive({
    if (!is.null(input$hot)) {
      DF = hot_to_r(input$hot)
    } else if (!is.null(cache_tbl)) {
      DF = hot_to_r(cache_tbl)
      cache_tbl <<- NULL
    } else {
      DF = data.frame(val = 1:10, bool = TRUE, nm = LETTERS[1:10],
                      dt = seq(from = Sys.Date(), by = "days", length.out = 10),
                      stringsAsFactors = F)
    }
    DF
  })

  output$hot <- renderRHandsontable({
    DF = data()
    if (!is.null(DF))
      rhandsontable(DF, useTypes = as.logical(input$useType), stretchH = "all")
  })
}

enableBookmarking(store = "url")
shinyApp(ui, server)
@nickforr
Copy link

@nickforr nickforr commented Nov 16, 2016

@jrowen wondered if you'd tried this lately as I don't seem to get your fix to work anymore (pretty sure it was working a few weeks ago)?

@nickforr
Copy link

@nickforr nickforr commented Nov 16, 2016

Just to update, this may be an issue with the specific browser. Just re-tested this using Safari and works ok (earlier problem was using IE on a windows machine).

@wch
Copy link
Collaborator

@wch wch commented Jan 3, 2017

You may have been running into the ~2000 character URL limit on IE:
http://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers

For cases like this, it makes sense to use server-side bookmarking instead of url-encoded bookmarking.

@wch wch closed this Jan 3, 2017
@wch wch removed the targeted label Jan 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.