简体   繁体   English

DT::datatable - 选择要删除的行并在没有 Shiny 的情况下写入 csv

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

I've managed to create a markdown document, that doesn't call Shiny, to:我已经设法创建了一个不调用 Shiny 的降价文档:

  • load in a dataframe加载数据帧
  • edit cells within a table编辑表格中的单元格
  • write edited output to a csv.将编辑后的输出写入 csv。

Is it possible to select rows for deletion, and remove these rows from the outputted csv, without using shiny?是否可以选择要删除的行,并从输出的 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'
                            )
              )
```

Any help would be much appreciated任何帮助将非常感激

Here is a way which uses the JavaScript library CellEdit .这是一种使用 JavaScript 库CellEdit 的方法

Download the file dataTables.cellEdit.js .下载文件dataTables.cellEdit.js

Some custom CSS is needed.需要一些自定义 CSS。 Save the following CSS code in a file dataTables.cellEdit.css , put it in the same folder that contains dataTables.cellEdit.js .将以下 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;
}

Now, here is the R code to put in your chunk edit_table :现在,这是要放入块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

Here I put the two dataTables.cellEdit files in the folder of the R Markdown document ( path <- "." ).这里我把两个dataTables.cellEdit文件放在 R Markdown 文档的文件夹中( path <- "." )。

You can edit a cell by clicking on it, and delete a row by double-clicking on it.您可以通过单击一个单元格来编辑它,并通过双击它来删除一行。

在此处输入图片说明


EDIT: better row deletion编辑:更好的行删除

Deleting a row by double-click is not very nice.通过双击删除一行不是很好。 This conflicts with the cell editing.这与单元格编辑冲突。 Here is another way, with which one can delete a row with a right-click.这是另一种方法,可以通过右键单击删除一行。 It uses the JavaScript library jQuery-contextMenu .它使用 JavaScript 库jQuery-contextMenu

Go here .这里 Download the files jquery.contextMenu.min.js , jquery.contextMenu.min.css , and jquery.ui.position.js .下载文件jquery.contextMenu.min.js,jquery.contextMenu.min.cssjquery.ui.position.js。 Put them in the path folder.将它们放在path文件夹中。 Also download the four font files context-menu-icons.eot , context-menu-icons.ttf , context-menu-icons.woff and context-menu-icons.woff2 , and put them in the subfolder font of the path folder.还要下载四个字体文件context-menu-icons.eotcontext-menu-icons.ttfcontext-menu-icons.woffcontext-menu-icons.woff2 ,并将它们放在path文件夹的子文件夹font中。 Now, here is the code:现在,这里是代码:

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)

And now, you can delete the rows by right-clicking:现在,您可以通过右键单击来删除行:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM