【问题标题】:How to reset to default a reactive rhandsontable?如何将反应性rhandsontable重置为默认值?
【发布时间】:2020-01-01 19:08:35
【问题描述】:

我正在构建一个应用程序,其中一个 2×2 表包含一些用于进一步计算的值。这些值可以由用户更新,用户可以恢复到原始值。

我正在尝试使用一个操作按钮来实现它,该按钮会将表格重置为其原始值,但表格不会更新。这是一个简化的例子:

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)

rhandsontable 是否可以通过actionButton 进行响应和更新?

【问题讨论】:

    标签: r shiny rhandsontable


    【解决方案1】:

    欢迎来到 Stackoverflow!

    这是一个工作示例(降低了复杂性):

    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)
    

    【讨论】:

    • 感谢您的提示。当条目更改时,我有一个 observeEvent 来计算 rhandsontable 中的一些单元格。一起使用时,rhandsontable 不会随着您的回答而重置。你有什么建议吗?
    • 很难说没有任何例子。您可能想发布一个新问题来详细说明您的案例。
    • 嗨@ismirsehregal 谢谢,我在这里添加了我的问题:stackoverflow.com/questions/63361539/…
    猜你喜欢
    • 1970-01-01
    • 2020-06-24
    • 2012-11-12
    • 1970-01-01
    • 2020-12-01
    • 2018-11-18
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多