通过编辑 table and/or eventReactive 更新 handsontable
Update handsontable by editing table and/or eventReactive
我在 Shiny 应用程序中使用 rhandsontable
包,它应该具有以下功能:
- 计算中使用的数据可以随机生成,由
actionButton
(以及应用程序启动时)调用
- 用户可以通过handsontable对象手动编辑数据
- 手动编辑后应该可以重新生成随机数据,调用新的计算
以下应用程序完全符合我的要求,但我不知道如何摆脱全局变量 did_recalc
。这是一个最小的例子,其中数据由两个相加的数值组成。
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
条件,那么编辑 table 仍然会调用(正确的)计算。但是,如果按下 "recalc"(在完成一些手动编辑之后),则 "recalc" 按钮只会生成新的随机值,但不会重新计算总和。
在我看来,input$table
只能通过手动编辑 table 对象来更改,而不关心通过 renderRHandsontable
给出的新值。因此我需要这个全局变量的 hack,它允许我跟踪用户是否刚刚重新生成数据(导致 input$table
是 "outdated")
有人知道如何在没有全局变量的情况下获得此示例的功能吗?
您可以将数据存储在 reactiveValues
中并让两个观察者更新它;一种是单击按钮,另一种是 table 是手动编辑的。
在您的 output$table
和 output$result
中,您只需使用 reactiveValues
中的数据即可。这是一个示例(与您发布的 ui.R
相同):
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)
})
})
我在 Shiny 应用程序中使用 rhandsontable
包,它应该具有以下功能:
- 计算中使用的数据可以随机生成,由
actionButton
(以及应用程序启动时)调用 - 用户可以通过handsontable对象手动编辑数据
- 手动编辑后应该可以重新生成随机数据,调用新的计算
以下应用程序完全符合我的要求,但我不知道如何摆脱全局变量 did_recalc
。这是一个最小的例子,其中数据由两个相加的数值组成。
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
条件,那么编辑 table 仍然会调用(正确的)计算。但是,如果按下 "recalc"(在完成一些手动编辑之后),则 "recalc" 按钮只会生成新的随机值,但不会重新计算总和。
在我看来,input$table
只能通过手动编辑 table 对象来更改,而不关心通过 renderRHandsontable
给出的新值。因此我需要这个全局变量的 hack,它允许我跟踪用户是否刚刚重新生成数据(导致 input$table
是 "outdated")
有人知道如何在没有全局变量的情况下获得此示例的功能吗?
您可以将数据存储在 reactiveValues
中并让两个观察者更新它;一种是单击按钮,另一种是 table 是手动编辑的。
在您的 output$table
和 output$result
中,您只需使用 reactiveValues
中的数据即可。这是一个示例(与您发布的 ui.R
相同):
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)
})
})