John Smith John Smith - 9 days ago 4
R Question

Shiny R Update a list box after confirming delete

This is probably a very basic question in regards to

shiny

I have a drop down list where a user clicks a button and the dropdown menu should update with one less item

Here is a toy example

library(shiny)
library(shinythemes)

getData<- function()
{
data <- c('A','B','C','D')
return (data)
}



ui <- fluidPage(

selectInput("names", "Select Data", getData(), multiple = FALSE),
actionButton("del","delete"),
bsModal("modalnew", "Approve Data For Reporting", "del", size = "medium",
HTML("Note: Confirm Deletion"),
br(),br(),
actionButton("delyes", "Yes"),
actionButton("delno", "No")
)

)

server <- function(input, output, session) {

# Reactive part that gets the data and populates the drop down
my_sel = reactive({
mydata = getData()
})

# Confirmation Menu to Approve the upload
observeEvent(input$delyes, {
toggleModal(session, "modalnew", toggle = "close")

# Delete an Item, Update the list box with one less item


})

}

shinyApp(ui = ui, server = server)


An item should disappear from the list.
I'm basically stuck on the reactive bit i guess.

In the project I'm doing separate from the toy example, the user reviews their data by first selecting their area, if they approve it a stored procedure in MySQL moves the data to another table and the list box should update to remove the data from the list box because it no longer needs to be approved

Answer

Use reactiveValues and updateSelectInput to change the Select Input.

server <- function(input, output, session) {

# Reactive part that gets the data and populates the drop down
v <- reactiveValues(
    mydata = getData()
)

# Confirmation Menu to Approve the upload
observeEvent(input$delyes, {
    toggleModal(session, "modalnew", toggle = "close")

    # Delete an Item, Update the list box with one less item
    v$mydata <- v$mydata[!v$mydata %in% input$names]

    #update the selection
    updateSelectInput(session, "names",
                      choices = v$mydata
    )

})   
}
Comments