我正在构建一个应用程序,其中 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
?