簡體   English   中英

從應用程序中的DT中刪除數據以及DT中的按鈕,閃亮的R

[英]removing data from DT in app and buttons in DT, shiny R

我在DT的第一欄添加了復選框。 他們的想法是讓用戶可以選擇刪除選定的行並將其放置在文件中。 按下按鈕“刪除”后,數據將發送到文件,但是我不知道如何更新DT,以獲取沒有那些已刪除行的數據。 碼:

library(shiny)
library(DT)
library(dplyr)


shinyApp(
  ui <- fluidPage(DT::dataTableOutput("ruless"),
                  fluidRow(column(4, offset = 1, actionButton("delete", "Delete", width = 200)))),

  server <- function(input, output) {

    values <- reactiveValues(data = NULL)

    values$data <- as.data.frame(
      cbind(c("a", "d", "b", "c", "e", "f"),
            c(1463, 159, 54, 52, 52, 220),
            c(0.7315, 0.0795, 0.027, 0.026, 0.026, 0.11)
      )
    )

    shinyInput = function(FUN, len, id, ...) {
      #validate(need(character(len)>0,message=paste("")))
      inputs = character(len)
      for (i in seq_len(len)) {
        inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...))
      }
      inputs
    }

    output$ruless <- DT::renderDataTable({
      datatable(
        data.frame(delete=shinyInput(checkboxInput,nrow(values$data),"cbox_"), values$data),
        selection="multiple",
        escape = FALSE,
        filter = list(position = 'top', clear = FALSE),
        extensions = list("ColReorder" = NULL, "Buttons" = NULL),
        options = list(
          dom = 'BRrltpi',
          autoWidth=TRUE,
          lengthMenu = list(c(10, 50, -1), c('10', '50', 'All')),
          ColReorder = TRUE,
          preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
          drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '),
          buttons = list(
            'copy',
            'print',
            list(
              extend = 'collection',
              buttons = c('csv', 'excel', 'pdf'),
              text = 'Download',
              selected = TRUE
            )
          )
        )
      )
    })


    shinyValue = function(id, len) { 
      unlist(lapply(seq_len(len), function(i) {
        value = input[[paste0(id, i)]] 
        if (is.null(value)) NA else value 
      })) 
    } 


    observeEvent(input$delete, {
      checkbox_rules <- data.frame(selected=shinyValue("cbox_",nrow(values$data)))
      marked_rules <- as.data.frame(values$data[(which(checkbox_rules == TRUE)),])
      if (file.exists("delete_file.csv")){
        delete_file <- as.data.frame(read_csv2("delete_file.csv", col_names  = TRUE))
        delete_file <- as.data.frame(rbind(delete_file, marked_rules))
        delete_file <- delete_file[!duplicated(delete_file), ]
        write.csv2(delete_file, file = "delete_file.csv", sep=";", row.names = FALSE)
      }
      else{
        write.csv2(marked_rules, file = "delete_file.csv", sep=";", row.names = FALSE)
      }
    })

  }
)

我還想替換刪除按鈕,並重命名DT上方的其余按鈕。 我在想這樣的事情:

在此處輸入圖片說明

我有可能嗎? 預先感謝! 可能嗎?

您可以像這樣在觀察器中更新data.frame

      values$data <- values$data[!checkbox_rules,]

最后應該看起來像這樣

observeEvent(input$delete, {
      checkbox_rules <- data.frame(selected=shinyValue("cbox_",nrow(values$data)))
      marked_rules <- as.data.frame(values$data[(which(checkbox_rules == TRUE)),])
      values$data <- values$data[!checkbox_rules,]
      if (file.exists("delete_file.csv")){
        delete_file <- as.data.frame(read_csv2("delete_file.csv", col_names  = TRUE))
        delete_file <- as.data.frame(rbind(delete_file, marked_rules))
        delete_file <- delete_file[!duplicated(delete_file), ]
        write.csv2(delete_file, file = "delete_file.csv", sep=";", row.names = FALSE)
      }
      else{
        write.csv2(marked_rules, file = "delete_file.csv", sep=";", row.names = FALSE)
      }
    })

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM