1

In below MWE code, the action button "show" triggers a modal dialogue box with a table the user can manipulate, using rhandsontable. Works as intended, except the "reset" button doesn't work. Reset should reverse all user inputs into the table and bring back a 2 column, 5 row table of random variables.

How do I reset the table? I've been fooling around with this this morning and no luck yet.

This needs to stay in modalDialog by the way! Rhandsontable in a modal dialog box seems tricky but is tremendously functional for this model with many inputs.

See images at the bottom. First image shows what happens when first invoking the App and clicking the "Show" action button. Good - a 5 row, 2 column table appears. Second image is what shows after the user right clicks on the table to insert an additional row at the bottom, entering a 6 into the x and y columns of the 6th row. If you click to "Dismiss" the modal and click "Show" again, good -- the same modified table appears, as intended. Repeatedly clicking Dismiss/Show keeps bringing back the same modified 6 row table -- good! But if you hit "Reset", the modified table with the 6th added row keeps coming back. Should revert to a 5 row table. If the user has manually input many rows into the table and not just one additional row like in this example, reset should bring back a 5 row table.

library(shiny)
library(rhandsontable)

ui <- fluidPage(actionButton("show","Show"), 
                actionButton("reset","Reset"))

server <- function(input, output, session){
  
  dat <- reactiveVal(data.frame(x=runif(5),y=runif(5)))
  
  dat1 <- reactive({
    if(is.null(input$hot)){dat()} 
    else {as.data.frame(hot_to_r(input$hot))}
  }) # close reactive
  
  observeEvent(input$show,{showModal(modalDialog(rHandsontableOutput("hot")))})
  
  observeEvent(input$reset,{dat(data.frame(x=runif(5),y=runif(5)))})
  
  output$hot <- renderRHandsontable(rhandsontable(dat1()))
  
} # close Server

shinyApp(ui,server)

enter image description here

enter image description here

And below is an image showing the obscured table after making a change to it and hitting the "Show" button (the entire table should be appearing instead):

enter image description here

1 Answers1

0

Your condition if(is.null(input$hot)){dat()} is blocking the reset.

Please check the following:

library(shiny)
library(rhandsontable)

ui <- fluidPage(actionButton("show", "Show"),
                actionButton("reset", "Reset"))

server <- function(input, output, session) {
  dat <- reactiveVal(data.frame(x = runif(5), y = runif(5)))
  
  observeEvent(input$hot, {
    dat(as.data.frame(hot_to_r(input$hot)))
  })
  
  observeEvent(input$show, {
    showModal(modalDialog(rHandsontableOutput("hot")))
  })
  
  observeEvent(input$reset, {
    dat(data.frame(x = runif(5), y = runif(5)))
  })
  
  output$hot <- renderRHandsontable({
    # input$show
    rhandsontable(dat())
  })
  
} # close Server

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Technically it works. But now, when you click "Show" after having changed the table, only part of the table appears in the modal dialogue. To make the entire table appear, you have to click anywhere on the table. Is there any way to fix this without losing the complete rendering of the table? – Curious Jorge - user9788072 Jul 23 '21 at 11:13
  • We can work around this by rerendering the table after clicking `show`. The same effect can be seen in your example for a short period of time - might be a bug in `rhandsontable`. Please see my edit. – ismirsehregal Jul 23 '21 at 11:18
  • But now reset has stopped working. Hmmmm...does seem buggy – Curious Jorge - user9788072 Jul 23 '21 at 11:32
  • Yes - something is strange here. You might want to [file an issue](https://github.com/jrowen/rhandsontable/issues). – ismirsehregal Jul 23 '21 at 11:56
  • Link to related GitHub issue: https://github.com/jrowen/rhandsontable/issues/387 – ismirsehregal Jul 27 '21 at 07:52