简体   繁体   中英

Unable to reset reactive rhandsontable observeEvent

I have been using R for awhile but R shiny seems like a completely foreign language to me.

I have an RShiny app utilizing the rhandsontable, which takes input from both another column in the rhandsontable and outside the rhandsontable to calculate a new column. Currently, the table does update everything when any entry from within the table is modified, but I'd like to minimize the risk of user error as much as possible, either by making the rhandsontable reactive to both changes inside and outside the table (preferred) or reset when a button is clicked.

The example below is modified from How to reset to default a reactive rhandsontable? but reproduced my issue. As soon as I add an observeEvent for the rhandsontable, even before I call the external input, the reset button no longer works.

To make the observeEvent reactive to both changes in the external input and the table changes, I tried to make a reactive input (eg, listener <- reactive(c(input$two_by_two$changes$changes, input$reset_input)) ), put them both in the observeEvent (eg, input$two_by_two$changes$changes | input$reset_input ), which both result in Warning: Error in do.call: second argument must be a list error.

library(shiny)
library(rhandsontable)

server <- shinyServer(function(input, output, session) {
  FACTOR <- reactive(input$factor_val)
  DF <- data.frame(A = c(1, 2), B = c(2, 4), row.names = c("C", "D"))
  DF1 <- reactiveValues(data=DF)
  output$two_by_two <- renderRHandsontable({
    input$reset_input 
    rhandsontable(DF1$data)
  })
  observeEvent(input$two_by_two$changes$changes,
               {DF1$data <- hot_to_r(input$two_by_two)
               DF1$data[,2] = DF1$data[,1]*FACTOR() }) })


ui <- shinyUI(fluidPage(
  actionButton(inputId = "reset_input", label = "Reset"),
  br(),
  numericInput("factor_val","Multiplier Value",min=0,max=10,value=2),
  rHandsontableOutput("two_by_two")
))

shinyApp(ui, server)

Thanks for any help again.

The reason you were not able to reset was that you had changed the reactive dataframe, but were never resetting it back. I added a new observe event to handle that. Please see if this is what you are looking for.

library(shiny)
library(rhandsontable)

server <- shinyServer(function(input, output, session) {
  FACTOR <- reactive(input$factor_val)
  DF <- data.frame(A = c(1, 2), B = c(2, 4), row.names = c("C", "D"))
  DF1 <- reactiveValues(data=DF)
  observeEvent(input$reset_input, {
    DF1$data <- DF
  })
  
  output$two_by_two <- renderRHandsontable({
    #input$reset_input
    rhandsontable(DF1$data)
  })
  observeEvent(input$factor_val, {
               req(input$factor_val)
               DF1$data <- hot_to_r(req({input$two_by_two}))
               DF1$data[,2] = DF1$data[,1]*FACTOR()
               })
})

ui <- shinyUI(fluidPage(
  actionButton(inputId = "reset_input", label = "Reset"),
  br(),
  numericInput("factor_val","Multiplier Value",min=0,max=10,value=2),
  rHandsontableOutput("two_by_two")
))

shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM