[英]Shiny R Resetting a dataTableOutput
我在Shiny中有一個Crud數據庫應用程序,其中用戶在下拉列表中選擇一個對象,然后單擊一個按鈕,然后從mysql數據庫中刪除數據。 用戶單擊代碼DT::dataTableOutput("reviewdata")
單擊查看按鈕時看到數據。 當用戶決定要刪除數據時,請按下按鈕,然后執行以下代碼,其中my_sel$mydata
是一個函數,用於重新查詢表以重新填充下拉框
#update the selection in the drop down box
updateSelectInput(session, "dropdownbox", choices = my_sel$mydata)
如前所述,當單擊檢查按鈕時,數據首先在屏幕上顯示給用戶。 當他們刪除項目時,下拉框會將其從列表中刪除(因為該項目不再可刪除)
我的問題是
謝謝
這是一個如何在Shiny中使用reactive
表達式的示例。 該應用程序顯示可用表的列表。 如果用戶選擇一個表,則會出現幾個按鈕以顯示Review
Delete
表”。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.