通过编辑表和/或事件来更新handontable [英] Update handsontable by editing table and/or eventReactive

查看:77
本文介绍了通过编辑表和/或事件来更新handontable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Shiny应用程序中使用rhandsontable软件包,该软件包应具有以下功能:

I am using the rhandsontable package in a Shiny app which should have the following functionality:

  • 计算中使用的数据可以随机生成,由actionButton调用(以及应用启动时)
  • 用户可以通过可操作的对象手动编辑数据
  • 手动编辑后,应该可以重新生成随机数据,从而进行新的计算
  • the data used in the calculation can be randomly generated, invoked by an actionButton (and when the app starts)
  • the data can be manually edited by the user via the handsontable object
  • after manual editing it should be possible to re-generate random data, invoking a new calculation

以下应用程序完全可以实现我想要的功能,但是我无法弄清楚如何摆脱全局变量did_recalc.这是一个最小的示例,其中数据由两个相加的数值组成.

The following app does exactly that what I want, but I could not figure it out how to get rid of the global variable did_recalc. It is a minimal example, where the data consists of two numeric values which are summed up.

library(shiny)
library(rhandsontable)

did_recalc <- FALSE

ui <- fluidPage(
  rHandsontableOutput('table'),
  textOutput('result'),
  actionButton("recalc", "generate new random vals and calculate")
)

server <- function(input,output,session)({

  dataset_generator <- eventReactive(input$recalc, {
    df <- as.data.frame(runif(2))
    output$table <- renderRHandsontable({rhandsontable(df)})
    did_recalc <<- TRUE
    df
  }, ignoreNULL = FALSE)

  output$result <- renderText({ 
    df <- dataset_generator()
    if (!is.null(input$table) && !did_recalc) 
      df <- hot_to_r(input$table)
    did_recalc <<- FALSE
    sum(df)
  })
}) 

shinyApp(ui = ui, server = server)

如果我删除了output$result <- ...中的!did_recalc条件,则编辑表仍会调用(正确的)计算.但是,如果按下重新计算"(在完成一些手动编辑之后),则重新计算"按钮只会生成新的随机值,而不会重新计算总和.

If I remove the !did_recalc condition within output$result <- ... then editing the table still invokes a (correct) calculation. But if "recalc" is pressed (after some manual editing was done), then the "recalc" button just generates new random values, but without recalculating the sum.

在我看来,input$table只能通过手动编辑表对象来更改,而不关心通过renderRHandsontable给出的新值.因此,我需要使用带有全局变量的hack,这可以让我跟踪用户是否只是重新生成了数据(因为input$table是过时的")

It seems to me, that input$table can just be changed by manual edits of the table object and does not care about new values given via renderRHandsontable. Hence I need this hack with the global variable, which allows me to track if the user just re-generated the data (causing that input$table is "outdated")

有人知道如何在没有全局变量的情况下获取此示例的功能吗?

Has anybody an idea how to get the functionality of this example without the global variable?

推荐答案

您可以将数据存储在reactiveValues中,并由两个观察者对其进行更新;一种是单击按钮,另一种是用手编辑表格.

You could store the data in a reactiveValues and have two observers updating it; one if the button is clicked, one if the table is edited by hand.

然后在output$tableoutput$result中,只需使用reactiveValues中的数据.这是一个示例(与您发布的ui.R相同):

In your output$table and output$result, you then just need to use the data that is in the reactiveValues. Here's an example (same ui.R as you posted):

server <- function(input,output,session)({
  values <- reactiveValues(data=as.data.frame(runif(2)))

  observe({
    input$recalc
    values$data <- as.data.frame(runif(2))
  })

  observe({
    if(!is.null(input$table))
     values$data <- hot_to_r(input$table)
  })


  output$table <- renderRHandsontable({
    rhandsontable(values$data)
    })


  output$result <- renderText({ 
    sum(values$data)
  })
}) 

这篇关于通过编辑表和/或事件来更新handontable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆