簡體   English   中英

DT::datatable - 選擇要刪除的行並在沒有 Shiny 的情況下寫入 csv

[英]DT::datatable - Select rows for deletion and write csv WITHOUT Shiny

我已經設法創建了一個不調用 Shiny 的降價文檔:

  • 加載數據幀
  • 編輯表格中的單元格
  • 將編輯后的輸出寫入 csv。

是否可以選擇要刪除的行,並從輸出的 csv 中刪除這些行,而不使用閃亮的?

---
title: "Test"
output: html_document
    
---

```{r setup, include=FALSE}

library(tidyverse)
library(DT)

```

```{r edit_table, echo = FALSE}   

                            
DT::datatable(head(mtcars, 10),
              editable = 'row', 
              extensions = c('Buttons'),
              options = list(lengthChange = FALSE,
                            #pageLength = 8,
                            scroller = TRUE,
                            scrollY = 500, 
                            scrollX = T,
                            #searching = TRUE,
                            dom = 'Blfrtip',
                            buttons = 'csv'
                            )
              )
```

任何幫助將非常感激

這是一種使用 JavaScript 庫CellEdit 的方法

下載文件dataTables.cellEdit.js

需要一些自定義 CSS。 將以下 CSS 代碼保存在文件dataTables.cellEdit.css 中,並將其放在包含dataTables.cellEdit.js的同一文件夾中。

.my-input-class {
  padding: 3px 6px;
  border: 1px solid #ccc;
  border-radius: 4px;
  width: 60px;
}

.my-confirm-class {
  padding: 3px 6px;
  font-size: 12px;
  color: white;
  text-align: center;
  vertical-align: middle;
  border-radius: 4px;
  background-color: #337ab7;
  text-decoration: none;
}

.my-cancel-class {
  padding: 3px 6px;
  font-size: 12px;
  color: white;
  text-align: center;
  vertical-align: middle;
  border-radius: 4px;
  background-color: #a94442;
  text-decoration: none;
}

現在,這是要放入塊edit_table的 R 代碼:

callback <- JS(
  "function onUpdate(updatedCell, updatedRow, oldValue) {}",
  "table.MakeCellsEditable({",
  "  onUpdate: onUpdate,",
  "  inputCss: 'my-input-class',",
  "  confirmationButton: {",
  "    confirmCss: 'my-confirm-class',",
  "    cancelCss: 'my-cancel-class'",
  "  }",
  "});",
  "$('#mytable tr').on('dblclick', function(){",
  "  table.row(this).remove().draw();",
  "});"
)
dtable <- datatable(
  head(mtcars, 10),
  elementId = "mytable",
  extensions = "Buttons",
  callback = callback,
  options = list(
    lengthChange = FALSE,
    scroller     = TRUE,
    scrollY      = 500, 
    scrollX      = TRUE,
    dom          = "Blfrtip",
    buttons      = "csv"
  )
)
path <- "." # path to folder containing dataTables.cellEdit.(js|css)
dep <- htmltools::htmlDependency(
  "CellEdit", "1.0.19", path, 
  script = "dataTables.cellEdit.js", 
  stylesheet = "dataTables.cellEdit.css", 
  all_files = FALSE)
dtable$dependencies <- c(dtable$dependencies, list(dep))
dtable

這里我把兩個dataTables.cellEdit文件放在 R Markdown 文檔的文件夾中( path <- "." )。

您可以通過單擊一個單元格來編輯它,並通過雙擊它來刪除一行。

在此處輸入圖片說明


編輯:更好的行刪除

通過雙擊刪除一行不是很好。 這與單元格編輯沖突。 這是另一種方法,可以通過右鍵單擊刪除一行。 它使用 JavaScript 庫jQuery-contextMenu

這里 下載文件jquery.contextMenu.min.js,jquery.contextMenu.min.cssjquery.ui.position.js。 將它們放在path文件夾中。 還要下載四個字體文件context-menu-icons.eotcontext-menu-icons.ttfcontext-menu-icons.woffcontext-menu-icons.woff2 ,並將它們放在path文件夾的子文件夾font中。 現在,這里是代碼:

library(DT)
library(htmltools)
callback <- JS(
  "function onUpdate(updatedCell, updatedRow, oldValue) {}",
  "table.MakeCellsEditable({",
  "  onUpdate: onUpdate,",
  "  inputCss: 'my-input-class',",
  "  confirmationButton: {",
  "    confirmCss: 'my-confirm-class',",
  "    cancelCss: 'my-cancel-class'",
  "  }",
  "});",
  "$.contextMenu({",
  "  selector: '#mytable tr',", 
  "  trigger: 'right',",
  "  autoHide: true,",
  "  items: {",
  "    delete: {",
  "      name: 'Delete this row',", 
  "      icon: 'delete',", 
  "      callback: function(itemKey, opts, e){",
  "        table.row(this).remove().draw();",
  "      }",
  "    }",
  "  }",
  "});"
)
dtable <- datatable(
  head(mtcars, 10),
  elementId = "mytable",
  extensions = "Buttons",
  callback = callback,
  options = list(
    lengthChange = FALSE,
    scroller     = TRUE,
    scrollY      = 500, 
    scrollX      = TRUE,
    dom          = "Blfrtip",
    buttons      = "csv"
  )
)
path <- "." # path to folder containing the js and css files, and the font folder
dep1 <- htmlDependency(
  "CellEdit", "1.0.19", path, 
  script = "dataTables.cellEdit.js", 
  stylesheet = "dataTables.cellEdit.css", 
  all_files = FALSE)
dep2 <- htmlDependency(
  "jQuery-contextMenu", "2.9.2", path, 
  script = "jquery.contextMenu.min.js", 
  stylesheet = "jquery.contextMenu.min.css", 
  all_files = FALSE)
dep3 <- htmlDependency(
  "jQuery-ui-position", "1.12.1", path, 
  script = "jquery.ui.position.js", 
  all_files = FALSE)
tagList(dtable, dep1, dep2, dep3)

現在,您可以通過右鍵單擊來刪除行:

在此處輸入圖片說明

暫無
暫無

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

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