3

For one of my Rshiny app, I am referring to this R shiny code from StackOverflow: In R Shiny, how do you reset data (reversing all manual inputs) in rhandsontable using an actionButton?

When we use the package "rhandsontable," the following code displays the DataTable whenever we click the "show" button. Although the rhandsontable package includes a default context menu, it can be customized using the "hot_context_menu" code.

issue:

The problem is with the 'undo' option in the context menu (right-click on the data table). The option 'undo' enabled to work only when we adjust alignment (right-click on the data table), but not (disabled) when we remove a row (Remove row), "insert row below," and so on.

Could someone help me to fix this issue?

original code

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)

Edited Code

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({
    rhandsontable(dat())
  })
  outputOptions(output, "hot", suspendWhenHidden = FALSE) 
  
} # close Server

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Kevin Tracey
  • 154
  • 1
  • 16
  • This seems to be an [issue](https://github.com/jrowen/rhandsontable/issues/375) with the shiny implementation - it's working fine when used without shiny: `rhandsontable(data.frame(x = runif(5), y = runif(5)))`. – ismirsehregal Feb 07 '22 at 16:02
  • Are you interested in a workaround so, that the reset button is able to remove added rows? – ismirsehregal Feb 07 '22 at 16:07
  • @ismirsehregal, Yes, that will much assist me in resolving this problem. Please assist me in finding a workaround. – Kevin Tracey Feb 07 '22 at 18:33
  • Just set `outputOptions(output, "hot", suspendWhenHidden = FALSE)` after assigning the `rhandsontable` to `output$hot`, to make the reset button work regarding additional / removed rows. By default re-rendering isn't triggered for hidden outputs. – ismirsehregal Feb 07 '22 at 20:20
  • @ismirsehregal, I have tried this functionality several times and it still doesn't work. In my original post, under "Edited Code," I included the updated(edited) code. Could you assist me with this? – Kevin Tracey Feb 08 '22 at 03:34
  • With the "edited code" the table is back to 5 rows after pressing reset (if the table was modified). – ismirsehregal Feb 08 '22 at 08:08
  • @ismirsehregal, Thank you very much. However, I am looking for a solution that uses the UNDO option in the context menu rather than the reset option. Could you help me with that? – Kevin Tracey Feb 08 '22 at 09:09
  • @ismirsehregal, Could you help me on this? – Kevin Tracey Feb 09 '22 at 05:58
  • As the issue I linked above suggests, I think this is either a implementation limitation or a bug and hopefully the package maintainer will look into it. I don't have a workaround for this. – ismirsehregal Feb 09 '22 at 08:27

0 Answers0