Delete row of DT data table in Shiny app

断了今生、忘了曾经 提交于 2019-12-21 05:16:47

问题


I have a shiny app that displays data frame data in a DT table. In the app I have a button that I when clicked will delete the selected rows. It works the first time I select rows and click the delete button but after clicking again the wrong rows are deleted and any previously deleted rows reappear. I'm assuming this is because it reloads the data frame (from a csv) when I call DT::renderDataTable().

How can I re render the table after deleting a selected row from the the data frame?


回答1:


This could get you started:

ui.R

    library(shiny)
    library(DT)
    shinyUI(fluidPage(
       titlePanel("Delete rows with DT"),
              sidebarLayout(
                sidebarPanel(
                    actionButton("deleteRows", "Delete Rows")
                ),
                mainPanel(
                   dataTableOutput("table1")
                )
              )
    ))

server.R

    library(shiny)
    library(DT)
    library(dplyr)
    df <- data.frame(x = 1:10, y = letters[1:10])

    shinyServer(function(input, output) {
            values <- reactiveValues(dfWorking = df)

           observeEvent(input$deleteRows,{

                    if (!is.null(input$table1_rows_selected)) {

                            values$dfWorking <- values$dfWorking[-as.numeric(input$table1_rows_selected),]
                    }
            })

            output$table1 <- renderDataTable({
                    values$dfWorking
            })

    })


来源:https://stackoverflow.com/questions/39136385/delete-row-of-dt-data-table-in-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!