DT::datatable - Select 行删除并写入 csv WITHOUT Shiny

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

我已经成功地创建了一个不调用 Shiny 的降价文档:

是否可以删除 select 行,并从输出的 csv 中删除这些行,而不使用 shiny?

---
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[=59= 的同一文件夹中].

.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;
}

现在,这是要放入块中的 R 代码 edit_table:

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.

here。下载文件 jquery.contextMenu.min.jsjquery.contextMenu.min.cssjquery.ui.position.js.将它们放在 path 文件夹中。同时下载四个字体文件 context-menu-icons.eot, context-menu-icons.ttf, context-menu-icons.woffcontext-menu-icons.woff2,并将它们放在子文件夹fontpath 文件夹。现在,这是代码:

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)

现在,您可以通过右键单击删除行: