Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
892 views
in Technique[技术] by (71.8m points)

r - Delete row of DT data table in Shiny app

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
            })

    })

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...