Shiny: How to reset rhandson table to default?

1.2k views Asked by At

I have a small app like this:

require(shiny)
require(shinyjs)
require(rhandsontable)

shinyApp(ui = fluidPage(useShinyjs(),
                        div(id = 'div1',
                            titlePanel("RHOT - Form"),
                            fluidRow(column(width = 3,selectizeInput("Trialid","What Iteration is this?",choices = c('1','2-3','4-7','8-15'))),
                                     column(width = 3,textInput("Techie_Name","Your Name",value='EE')),
                                     column(width = 3,textInput("lab_id","LAB ID",value='NA')),
                                     column(width = 3,textInput("email","Your Email ID",value='[email protected]'))
                            ),
                            h4('Observations:'),
                            rHandsontableOutput("handsontable_obs"),
                            actionButton("SaveObs", "Save Observations")
                            ),
                        shinyjs::hidden(div(id = 'SubmitMsg',
                                            h3("Thanks for submitting the Observations!"),
                                            actionLink('addNextObs',"Add Another Observation"))
                                        )
                        ),
         server = function(input, output,session){
           output$handsontable_obs = renderRHandsontable({
             rhandsontable(data.frame(Obs_itr = c(1:5),
                                      Val1 = rep(0,5),
                                      Val2 = rep(0,5)))
           })

           observeEvent(input$SaveObs,{
             shinyjs::reset("div1")
             shinyjs::hide("div1")
             shinyjs::show("SubmitMsg")
           })

           observeEvent(input$addNextObs,{
             shinyjs::show("div1")
             shinyjs::hide("SubmitMsg")
           })
         }
)

When I run it, I can edit the input fields as well as the tables. Upon hitting the save button, this div resets (using shinyjs::reset), hides, and a hidden thank you div shows up. Clicking another action link on the 2nd div brings the original one back on. Now, ass the input fields are reset to their default values, except the handsontable.

Question is, how do I ensure the handsontable resets to default values along with the other input fields?

1

There are 1 answers

0
Bernhard Klingenberg On

Adding a reactiveValue and a bit more detail on the rhandsontable gets the job done, but this may not be very efficient:

shinyApp(ui = fluidPage(useShinyjs(),
                        div(id = 'div1',
                            titlePanel("RHOT - Form"),
                            fluidRow(column(width = 3,selectizeInput("Trialid","What Iteration is this?",choices = c('1','2-3','4-7','8-15'))),
                                     column(width = 3,textInput("Techie_Name","Your Name",value='EE')),
                                     column(width = 3,textInput("lab_id","LAB ID",value='NA')),
                                     column(width = 3,textInput("email","Your Email ID",value='[email protected]'))
                            ),
                            h4('Observations:'),
                            rHandsontableOutput("handsontable_obs"),
                            actionButton("SaveObs", "Save Observations")
                        ),
                        shinyjs::hidden(div(id = 'SubmitMsg',
                                            h3("Thanks for submitting the Observations!"),
                                            actionLink('addNextObs',"Add Another Observation"))
                        )
),
server = function(input, output,session){

  vals <- reactiveValues(reset=TRUE)

  output$handsontable_obs = renderRHandsontable({
    input$addNextObs
    if(isolate(vals$reset) | is.null(input$handsontable_obs)) {
      isolate(vals$reset <- FALSE)
      df <- data.frame(Obs_itr = c(1:5),
                       Val1 = rep(0,5),
                       Val2 = rep(0,5))
    } else df <- hot_to_r(input$handsontable_obs)
    rhandsontable(df)
  })

  observeEvent(input$SaveObs,{
    shinyjs::reset("div1")
    shinyjs::hide("div1")
    shinyjs::show("SubmitMsg")
    vals$reset <- TRUE
  })

  observeEvent(input$addNextObs,{
    shinyjs::show("div1")
    shinyjs::hide("SubmitMsg")
  })
}
)