Hello,
I have this table that is made by rhandsontable
. The table work as intended - all values that are accepted fall within the respective min and max of the row. However, I am having problem resetting the table. What should happen is that the table should be like the original table and I thought an action button with the corresponding table would do it. I am pretty sure something of the <<-
assignment is making this more difficult. I added a message to my button to at least see that it is triggering the event.
Any idea how I can get the table to truly reset? Essentially, changing df()
back to the original table on the reset button.
library(shiny)
library(rhandsontable)
ui <- fluidPage(rHandsontableOutput('table'),
actionButton("hard_reset","Reset!"))#,
x <- reactiveValues(transfer = NULL)
server <- function(input, output, session) {
df <- eventReactive(input$table, {
if (is.null(input$table)) {
dfOld <<-
df <-
data.frame(
Parameter = c('A', 'B', 'C'),
Min = c(10, 20, 30),
Max = c(20, 30, 40),
Selected1 = c(0, 0, 0),
Selected2 = c(0, 0, 0),
Selected3 = c(0, 0, 0),
Selected4 = c(0, 0, 0),
Selected5 = c(0, 0, 0)
)
} else {
df <- hot_to_r(input$table)
# Quality control
# Rule for Simulation 1:
if (all(df$Selected1 >= df$Min |
df$Selected1 == 0) & all(df$Selected1 <= df$Max)) {
df$Selected1 <- df$Selected1
} else {
df$Selected1 <- dfOld$Selected1
}
# Rule for Simulation 2:
if (all(df$Selected2 >= df$Min |
df$Selected2 == 0) & all(df$Selected2 <= df$Max)) {
df$Selected2 <- df$Selected2
} else {
df$Selected2 <- dfOld$Selected2
}
# Rule for Simulation 3:
if (all(df$Selected3 >= df$Min |
df$Selected3 == 0) & all(df$Selected3 <= df$Max)) {
df$Selected3 <- df$Selected3
} else {
df$Selected3 <- dfOld$Selected3
}
# Rule for Simulation 4:
if (all(df$Selected4 >= df$Min |
df$Selected4 == 0) & all(df$Selected4 <= df$Max)) {
df$Selected4 <- df$Selected4
} else {
df$Selected4 <- dfOld$Selected4
}
# Rule for Simulation 5:
if (all(df$Selected5 >= df$Min |
df$Selected5 == 0) & all(df$Selected5 <= df$Max)) {
df$Selected5 <- df$Selected5
} else {
df$Selected5 <- dfOld$Selected5
}
}
dfOld <<- df
}, ignoreNULL = F)
output$table <- renderRHandsontable({
if (is.null(df()))
return()
rhandsontable(df()) %>%
hot_col("Parameter", readOnly = TRUE) %>%
hot_validate_numeric(
col = 'Min',
min = 1,
max = 50,
allowInvalid = FALSE
) %>%
hot_validate_numeric(
col = 'Max',
min = 1,
max = 50,
allowInvalid = FALSE
)
})
observeEvent(input$hard_reset, {
reset_dialog <- modalDialog("A reset has occured")
showModal(reset_dialog)
x$transfer <-
data.frame(
Parameter = c('A', 'B', 'C'),
Min = c(10, 20, 30),
Max = c(20, 30, 40),
Selected1 = c(0, 0, 0),
Selected2 = c(0, 0, 0),
Selected3 = c(0, 0, 0),
Selected4 = c(0, 0, 0),
Selected5 = c(0, 0, 0))
df <- x$transfer
})
}
shinyApp(ui, server)