2

I'm building an app where a 2-by-2 table contains some values that are used for further computation. These values can be updated by user, and user would be able to get back to the original values.

I'm trying to achieve it with an action button that would reset the table to its original values, but the table does not update. This is a simplified example:

rm(list = ls())
library(shiny)
library(rhandsontable)
library(shinyjs)

server <- shinyServer(function(input, output, session) {
                          DF = data.frame(A = c(1, 2), B = c(3, 4), row.names = c("C", "D"))

                          vals <- reactiveValues(reset = FALSE)

                          ## Initiate table
                          previous <- reactive({DF})

                          myChanges <- reactive({
                                         if(is.null(input$two_by_two)) {
                                                        return(previous())
                                         } else if(!identical(previous(),
                                                                         input$two_by_two)){
                                         mytable <- as.data.frame(hot_to_r(input$two_by_two))
                                         mytable
                                         }
                                                })
                          output$two_by_two <- renderRHandsontable({
                                         if(isolate(vals$reset) | is.null(input$two_by_two)) {
                                         isolate(vals$reset <- FALSE)
                                         df <- DF
                                         } else df <- myChanges()
                                         rhandsontable(df)
                                         })

                          fctout = reactive({2*myChanges()})

                          output$chg_data = renderTable({fctout()}, rownames = TRUE)

                          observeEvent(input$reset_input, {
                                           shinyjs::reset("test")
                                           vals$reset <- TRUE
                                       })
                      })
############ UI
ui <- shinyUI(fluidPage(
                  shinyjs::useShinyjs(),
                  id = "test",
                  h4("A table:"),
                  actionButton(inputId = "reset_input",
                               label = "Use example"),
                  br(),
                  rHandsontableOutput("two_by_two"),
                  br(),
                  tableOutput(outputId = "chg_data")
              ))

shinyApp(ui, server)

Could the rhandsontable be reactive and updated by an actionButton?

Denis
  • 23
  • 3

1 Answers1

3

Welcome to Stackoverflow!

Here is a working example (reduced complexity):

library(shiny)
library(rhandsontable)

server <- shinyServer(function(input, output, session) {
  DF <- data.frame(A = c(1, 2), B = c(3, 4), row.names = c("C", "D"))

  output$two_by_two <- renderRHandsontable({
    input$reset_input # trigger rendering on reset
    rhandsontable(DF)
  })

  output$chg_data = renderTable({
    hot_to_r(req({input$two_by_two}))*2}, rownames = TRUE)
})


ui <- shinyUI(fluidPage(
  h4("A table:"),
  actionButton(inputId = "reset_input", label = "Reset"),
  br(),
  rHandsontableOutput("two_by_two"),
  br(),
  tableOutput(outputId = "chg_data")
))

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Thanks for the tip. I have an observeEvent to calculate some cells in the rhandsontable when an entry is changed. When used together, the rhandsontable does not reset with your answer. Do you have a suggestion? – shirleywu Aug 10 '20 at 23:10
  • Hard to tell without any example. You might want to post a new question elaborating on your case. – ismirsehregal Aug 11 '20 at 11:12
  • Hi @ismirsehregal thanks, I added my question here: https://stackoverflow.com/questions/63361539/unable-to-reset-reactive-rhandsontable-observeevent – shirleywu Aug 11 '20 at 15:34