2016-11-28 77 views
0

我在Shiny中有一個crud數據庫應用程序,用戶在一個下拉列表中選擇一個對象,單擊一個按鈕並從mysql數據庫中刪除數據。當用戶使用代碼DT::dataTableOutput("reviewdata")單擊查看按鈕時,用戶能夠看到數據。當用戶決定他們希望刪除他們按下一個按鈕的數據和下面的代碼執行,其中my_sel$mydata是一個函數重新查詢表重新填充下拉框Shiny R重置dataTableOutput

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

如前所述,當回顧按鈕點擊數據首先在屏幕上顯示給用戶。當他們刪除下拉框中從列表中刪除的項目(如它不再刪除)

我的問題是

  • 是否有表有類似的功能,所以表應該是空的,因爲我們已經刪除了這些數據,因此,應該沒有顯示

感謝

+0

只需使用被動數據集並在沒有要顯示的數據時返回「NULL」。數據表將自動更新。 – Geovany

+0

嗨@Geovany你能給我一個你想到的例子嗎?如果你有一個例子,你可以給我一個例子嗎? –

回答

1

下面是如何在閃亮使用reactive表達的例子。這個應用程序顯示可用表的列表。如果用戶選擇一個表格,則表格的ReviewDelete

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)