删除最后一行数据表

时间:2019-10-06 06:59:20

标签: r shiny dt

我有一个将数据添加到数据表的代码。当我单击删除按钮时,我希望逻辑删除DT的最后一行。我尝试了各种逻辑,但没有起作用。  有人可以帮忙吗

    library(shiny)
    library(shinyjs)

    fields <- c("fy","quarter","month", "due_date","actual_date")

###########################################
    ui <- fluidPage(

  # Application title
     titlePanel("MCA Data Entry"),

  # Sidebar with reactive inputs
       sidebarLayout(
         sidebarPanel(
          textInput("fy","Financial Year"),
          selectInput("quarter","Quarter",c("Q1","Q2","Q3","Q4")),
          dateInput("month","Month of Account", format="MM, yyyy"),
          dateInput("due_date", "Due date for submission", format="dd-mm-yyyy"),
          dateInput("actual_date","Actual date of submission",format="dd-mm-yyyy"),
          actionButton("save","Add"),
          actionButton("reset","Delete")

         ),

    # a table of reactive outputs
         mainPanel(
         mainPanel(

          DT::dataTableOutput("responses", width = 600), tags$hr()

          )
          )
          )
           )
      server <- function(input, output,session) {

  #create a data frame called responses
      saveData <- function(data) {
        data <- data.frame(
         fy=data["fy"],
         quarter=data["quarter"],
         month=as.Date(as.numeric(data[["month"]]),"1970-01-01"),
         due_date=as.Date(as.numeric(data[["due_date"]]),"1970-01-01"),
         actual_date=as.Date(as.numeric(data[["actual_date"]]),"1970-01-01")
         )
         if (exists("responses")) {
          responses <<- rbind(responses, data)
          } else {
      responses <<- data
    }
  }

  loadData <- function() {
    if (exists("responses")) {
      responses
    }
  }


  # Whenever a field is filled, aggregate all form data
  #formData is a reactive function
  formData <- reactive({
    data <- sapply(fields, function(x) input[[x]])
    data
    print(data)
  })

  # When the Save button is clicked, save the form data
  observeEvent(input$save, {
    saveData(formData())
  },priority=1)

  # Show the previous responses
  # (update with current response when save is clicked)
  output$responses <- DT::renderDataTable({
    input$save
    datatable(loadData(),rownames=FALSE,options = list(sDom  = '<"top"><"bottom">'))
  })     
}
shinyApp(ui,server)

我想在按下删除按钮时删除最后一行,或者如果无法删除整个表并重新开始。我尝试了各种逻辑,这些逻辑可以删除数据,而不能删除行。通过我的逻辑将其删除后,将保留空行。

1 个答案:

答案 0 :(得分:0)

这是在数据表中集成按钮的一种方式:

library(DT)

datatable(iris[1:5,],
          extensions = 'Buttons',
          options = list(
            dom = 'Bfrtip',
            buttons = list(
              list(
                extend = "collection",
                text = 'Delete last row',
                action = DT::JS(c(
                  "function ( e, dt, node, config ) {",
                  "  var lastRow = dt.rows().count();",
                  "  dt.row(lastRow-1).remove().draw();",
                  "}"))
                )
              )
            )
          )

enter image description here


编辑

这是一种使用“闪亮”按钮的方法:

library(DT)
library(shiny)

ui <- fluidPage(
  actionButton("delete", "Delete last row"),
  br(),
  DTOutput("tbl")
)

server <- function(input, output){
  output[["tbl"]] <- renderDT({
    datatable(iris[1:5,],
              callback = JS(c(
                "$('#delete').on('click', function(){",
                "  var lastRow = table.rows().count();",
                "  table.row(lastRow-1).remove().draw();",
                "});"
              ))
    )    
  }, server = FALSE)
}

shinyApp(ui, server)