【问题标题】:Delete last row Data Table删除最后一行数据表
【发布时间】:2019-10-06 06:59:20
【问题描述】:

我有一个将数据添加到数据表的代码。当我单击删除按钮时,我想要一个逻辑来删除 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)

我想在按下删除按钮时删除最后一行,或者如果不可能删除整个表并重新开始。我尝试了各种逻辑,这些逻辑可以删除数据而不是行本身。通过我的逻辑删除后,空行仍然存在。

【问题讨论】:

  • 我正在使用手机,因此无法轻松输入,但我建议您将数据存储为反应值。参见解决方案here 例如。使用重置按钮上的observeEvent 更新它会更容易

标签: r shiny dt


【解决方案1】:

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

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();",
                  "}"))
                )
              )
            )
          )


编辑

这是一种带有闪亮按钮的方法:

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)

【讨论】:

  • 是否可以将它集成到我的 UI 中。意味着删除按钮是数据输入屏幕本身的一部分。
猜你喜欢
  • 1970-01-01
  • 2016-01-29
  • 1970-01-01
  • 1970-01-01
  • 2010-09-15
  • 2011-02-18
  • 1970-01-01
  • 2015-01-15
  • 1970-01-01
相关资源
最近更新 更多