I have an R Shiny app that copies a lot from this code: How to reset row selection in rhandsontable in Shiny app
I want to include a Remove Row button that should be disabled until the user clicks on a row in the table.
Then, when the user clicks on a row, the button should be enabled and if the user clicks that button, then the row will be deleted and the table should stay there but just without the row.
Finally, once the row is removed, I want the Remove Button to go back to being disabled until the user once again clicks on a row within the table.
Is there a way I can accomplish this? Thank you.
require(shiny)
require(rhandsontable)
require(shinyjs)
require(dplyr)
cars_data <- mtcars %>%
mutate(placement = row_number()) %>%
relocate(placement, .before = mpg)
shinyApp(
ui = fluidPage(
useShinyjs(),
actionButton(inputId = "remove_row", label = "Remove Row From Table"),
rHandsontableOutput("mytable")
),
server = function(input, output, session) {
output$mytable <- renderRHandsontable({
rhandsontable(
data = cars_data,
selectCallback = T
)
})
observeEvent(input$remove_row,{
selected_rhands_rows <- input$mytable_select$select$r
new_cars <- filter(cars_data, !(placement %in% cars_data[selected_rhands_rows, ]$placement))
cars_data <- new_cars %>% mutate(placement = as.numeric(seq(1:(nrow(cars_data) - length(selected_rhands_rows)))))
})
observe({
if(is.null(input$mytable_select$select$r)){
shinyjs::disable("remove_row")
}else{
shinyjs::enable("remove_row")
}
})
}
)
You can re-render the rhandsontable
inside your observeEvent
after removing the row and then disable the button. Notice that you also should put the data inside a reactiveValues
since it gets modified when clicking the button.
library(shiny)
library(rhandsontable)
library(shinyjs)
library(dplyr)
cars_data <- mtcars |>
mutate(placement = row_number()) |>
relocate(placement, .before = mpg)
shinyApp(
ui = fluidPage(
useShinyjs(),
actionButton(inputId = "remove_row", label = "Remove Row From Table", disabled = ''),
rHandsontableOutput("mytable")
),
server = function(input, output, session) {
rv <- reactiveValues(df = cars_data)
output$mytable <- renderRHandsontable({
rhandsontable(data = rv$df,
selectCallback = TRUE)
})
observe({
if (!is.null(input$mytable_select$select$r)) {
shinyjs::enable("remove_row")
}
})
observeEvent(input$remove_row, {
selected_rhands_rows <- input$mytable_select$select$r
rv$df <- rv$df |>
slice(-c(selected_rhands_rows)) |>
mutate(placement = row_number())
output$mytable <- renderRHandsontable({
rhandsontable(data = rv$df,
selectCallback = TRUE)
})
shinyjs::disable("remove_row")
})
}
)