简体   繁体   中英

Shiny R Resetting a dataTableOutput

I have a crud database application in Shiny where a user selects an object in a drop down list clicks a button and data is deleted from a mysql database. The user is able to see the data when they click review button using the code DT::dataTableOutput("reviewdata") . When the user decides they wish to delete the data they press a button and the following code executes where my_sel$mydata is a function that re-queries the table to re-populate the dropdown box

#update the selection in the drop down box
updateSelectInput(session, "dropdownbox", choices = my_sel$mydata)

As mentioned when the review button is clicked the data is displayed first on the screen to the user. When they delete it the dropdown box removes the item from the list (as its no longer available to delete)

My question is

  • Is there a similar feature for tables, so the table should be blank because we have deleted the data so there should be nothing to display

Thanks

Here is an example of how to use a reactive expression in Shiny. This app shows a list of available tables. If the user select one table a couple of buttons appear to Review of Delete the Table.

library(shiny)
library(DT)

ui <- fluidPage(
  title = 'Empty Table Example',
  fluidRow(
    column(4,
     uiOutput("dataAvailable_UI"),
     uiOutput("controls_UI")
    ),
    column(8, DT::dataTableOutput('reviewdata'))
  )
)

server <- function(input, output, session) {
  # similate the available tables in DB
  availableDatasets <- c("mtcars","iris", "cars", "trees")

  dataset <- reactive({
    input$deleteBT # to update when data is deleted

    # only return the corresponding table if user clicked on Review
    if (is.null(input$ReviewBT) || input$ReviewBT == 0)
      return(NULL)

    dataName <- isolate(input$dropdownbox)
    if (is.null(dataName) || !dataName %in% availableDatasets)
      return(NULL)

    # return the selected data
    get(dataName)

  })

  output$reviewdata = DT::renderDataTable(dataset())

  output$dataAvailable_UI <- renderUI({
    # no data is selected
    selectInput("dropdownbox", "Select a Table", 
                choices = c("", availableDatasets)) 
  })

  output$controls_UI <- renderUI({
    # only shows the buttons if a dataset is selected
    if (!is.null(input$dropdownbox) && nchar(input$dropdownbox) > 0)
      div(
        actionButton("ReviewBT", "Review Table"),
        actionButton("deleteBT", "Delete Table")
      )
  })

  observeEvent(input$deleteBT,{
    # delete data and update the selectInput
    dataName <- input$dropdownbox
    if (dataName %in% availableDatasets) {
      availableDatasets <<- availableDatasets[-match(dataName, availableDatasets)]
      updateSelectInput(session, "dropdownbox", choices = c("",availableDatasets))
    }
  })
}
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM