I want to update a dataframe with user input in an R Shiny app. The user selects a row of data, and then chooses the value to update the Species column with using selectInput
. The updates need to accumulate each time, i.e., the new updates update the previously updated data. I tried using reactiveValues
as per this answer, but couldn't get it to work.
library(shiny)
library(reactable)
library(tidyverse)
iris_df = iris %>%
mutate(
id = row_number(),
Species = as.character(Species)
)
ui <- fluidPage(
navbarPage(
"HELP!",
tabPanel(
"Iris",
sidebarLayout(
sidebarPanel(
selectInput("update_species", "Update species", choices = c("Rose", "Daffodil"))
),
mainPanel(fluidRow(reactableOutput("iris")))
)
)
)
)
server <- function(input, output) {
observeEvent(input$update_species, {
iris_df = iris_df %>%
mutate(Species = case_when(id == selected_row() ~ input$update_species, TRUE ~ Species))
})
selected_row = reactive(getReactableState("iris", "selected"))
output$iris = renderReactable({
reactable(
iris_df,
selection = "single",
)
})
}
shinyApp(ui = ui, server = server)