我创建了带有扩展子行的数据表(带复选框)。我有一个操作按钮,用于从数据集中删除选定的行,但我无法使其工作。
请向我提出有关如何从全局数据集中删除所选行的建议。
library(DT)
library(data.table)
library(Shiny)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl'), actionButton("button", "Delete")),
server = function(input, output) {
output$tbl = DT::renderDataTable(
datatable({
#Transform dataframe to data.table and turn the dataframe rowname into a data.table column called model
mtcars_dt = data.table(mtcars)
mtcars_dt[["model"]] = rownames(mtcars)
setcolorder(mtcars_dt,c(
which(colnames(mtcars_dt) %in% c("mpg","cyl","model")),
which(!colnames(mtcars_dt) %in% c("mpg","cyl","model"))
))
#Turn data table into a nested data.table by mpg, cyl
mtcars_dt <- mtcars_dt[, list(cars=list(.SD)), by = list(mpg,cyl)]
#configure datatable. Hide row number and cars columns [0,4] and enable details control on plus sign column[1]
#turn rows into child rows and remove from parent
cbind(' ' = '⊕', mtcars_dt)},
escape = -2, selection = 'multi',
options = list(
columnDefs = list(
list(visible = FALSE, targets = c(0,4)),
list(orderable = FALSE, className = 'details-control', targets = 1)
)
),
callback = JS("
table.column(1).nodes().to$().css({cursor: 'pointer'});
// Format cars object into another table
var format = function(d) {
if(d != null) {
var result = ('<table id=\"child_' + d[2] + '_' + d[3] + '\">').replace('.','_') + '<thead><tr>'
result += '<th> Delete</th>'
for (var col in d[4][0]) {
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
} else {
return '';
}
}
请查看以下屏幕截图以获得进一步说明。
由于 SJB