Skip to content

Issue restoring from bookmark URL #1378

@jrowen

Description

@jrowen

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)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions