Jef*_*hou 5 javascript jquery r shiny datatables-1.10
我无法在Shiny中复制数据表对象。当前,我可以在Shiny环境之外运行代码的数据表部分时显示想要的内容。但是,当我运行整个代码时,它没有显示子表。
library(DT)
library(data.table)
library(shiny)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
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,
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>'
for (var col in d[4]){
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
}else{
return '';
}
}
var format_datatable = function(d) {
var dataset = [];
for (i = 0; i < + d[4]['model'].length; i++) {
var datarow = [];
for (var col in d[4]){
datarow.push(d[4][col][i])
}
dataset.push(datarow)
}
var subtable = $(('table#child_' + d[2] + '_' + d[3]).replace('.','_')).DataTable({
'data': dataset,
'autoWidth': true,
'deferRender': true,
'info': false,
'lengthChange': false,
'ordering': true,
'paging': false,
'scrollX': false,
'scrollY': false,
'searching': false
});
};
table.on('click', 'td.details-control', function() {
var td = $(this), row = table.row(td.closest('tr'));
if (row.child.isShown()) {
row.child.hide();
td.html('⊕');
} else {
row.child(format(row.data())).show();
td.html('⊖');
format_datatable(row.data())
}
});")
)
)
}
)
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
这里的关键似乎是对象和数组之间的区别。当使用shiny时,row.data()是一个数组,它的第五个元素也是一个数组(这里我点击了主表中的第二行):
["2", "⊕", 22.8, 4, Array(2)]
Run Code Online (Sandbox Code Playgroud)
在闪亮的环境之外,row.data()看起来像这样:
["2", "⊕", 22.8, 4, Object]
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,第五个元素是一个对象!为什么会这样,我也说不出来。我想幕后使用的库的版本可能有所不同。
为了使其正常工作,我们需要进行 2 项更改:
1. 改变format():
var format = function(d) {
if(d != null) {
var result = ('<table id=\"child_' + d[2] + '_' + d[3] + '\">').replace('.','_') + '<thead><tr>'
for (var col in d[4][0]) {
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
} else {
return '';
}
}
Run Code Online (Sandbox Code Playgroud)
这里我们只是[0]在第4行添加了。如上所示,d[4]是一个数组。对于第二行数据,它由 2 个对象组成。(var col in d[4])将返回0and 1(对象的索引),而(var col in d[4][0])返回第一个对象的元素(即列名称)。
2. 改变format_datatable():
var format_datatable = function(d) {
var dataset = [];
for (i = 0; i <= d[4].length-1; i++) {
var datarow = $.map(d[4][i], function(value, index) {
return [value];
});
dataset.push(datarow);
}
// ...
// the rest did not change
}
Run Code Online (Sandbox Code Playgroud)
在这里,我们将表示为对象的每个汽车模型(因此 中的每个元素d[4])转换为使用 的数组$.map()。之后我们只需将此数组添加到dataset.
创建子表的代码需要这些数组。DataTables 处理数据的方式与其类型不同,可以在此处进行检查。
| 归档时间: |
|
| 查看次数: |
1574 次 |
| 最近记录: |