-
Notifications
You must be signed in to change notification settings - Fork 3.8k
How about a example of data tables child row? #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
You may try the new DT package: http://rstudio.github.io/DT/ Here is one example: http://rstudio.github.io/DT/002-rowdetails.html |
In the example below, same number of child rows (in this case 1 row) are present for every parent row. http://rstudio.github.io/DT/002-rowdetails.html Is there a way to add dynamic child rows to the parent row from another data frame? in R? meaning one parent row can have 6 (lets day) child rows based on data in parent row and another parent row can have 2 or may be no rows based on data in their respective parent rows. Thanks for your time! |
I just finished an example trying to do the same thing sort of.. Grouped mpg, cyl, disp together. Shows all matching cars under each group. There should be some way to iterate through nested levels of columns containing data.tables, but the javascript necessary to do this is beyond me.. |
Ok got this working.. Took mtcars and grouped it by mpg and cyl to create a data table. If you click on the + icon the child rows for the cars show up as another nested data table. I got up hung up for almost a day trying to figure out why datatable() was only working on the first set of cars. Turns out |
More complex example with multiple levels of nesting.. This assumes the first column of every nested table is a detail-control class (+) and the last column is the next level of nested data.
|
@davlee1972 Can you post the code to get my_data_dt in your 'more complex' example? I've been trying to wrap my head around it for over an hour and can't seem to get that example to run. Thanks for your examples! |
Here's an updated version, but it isn't perfect.. ` ui <- fluidPage(fluidRow(DT::dataTableOutput(width = "100%", "table"))) server <- function(input, output) { output$table = DT::renderDataTable({
}) shinyApp (ui = ui, server = server) |
Hi, I have taken the fantastic example from @davlee1972's mtcars_nested_datatable.R.txt and changed the table manipulation to dplyr/tidyr style and I have generalized the call back so that it is now plug'n'play for nested tables. Now my question is if it would be possible to get multiple layers of nesting for this example? Preferably automatically turning all nested tables recursively into child rows. library(DT)
library(tidyr)
library(dplyr)
library(tibble)
# nested data
mtcars_dt <- mtcars %>%
rownames_to_column("model") %>%
as_data_frame %>%
select(mpg, cyl, model, everything()) %>%
nest(-mpg, -cyl)
# add control column
data <- mtcars_dt %>% {bind_cols(data_frame(' ' = rep('⊕',nrow(.))),.)}
# get dynamic info and strings
nested_columns <- which(sapply(data,class)=="list") %>% setNames(NULL)
not_nested_columns <- which(!(seq_along(data) %in% c(1,nested_columns)))
not_nested_columns_str <- not_nested_columns %>% paste(collapse="] + '_' + d[") %>% paste0("d[",.,"]")
# The callback
# turn rows into child rows and remove from parent
callback <- paste0("
table.column(1).nodes().to$().css({cursor: 'pointer'});
// Format data object (the nested table) into another table
var format = function(d) {
if(d != null){
var result = ('<table id=\"child_' + ",not_nested_columns_str," + '\">').replace('.','_') + '<thead><tr>'
for (var col in d[",nested_columns,"]){
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
}else{
return '';
}
}
var format_datatable = function(d) {
var dataset = [];
for (i = 0; i < + d[",nested_columns,"]['model'].length; i++) {
var datarow = [];
for (var col in d[",nested_columns,"]){
datarow.push(d[",nested_columns,"][col][i])
}
dataset.push(datarow)
}
var subtable = $(('table#child_' + ",not_nested_columns_str,").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())
}
});"
)
datatable(
data,
escape = -2, # raw HTML in column 2
options = list(
columnDefs = list(
list(visible = FALSE, targets = c(0,nested_columns) ), # Hide row numbers and nested columns
list(orderable = FALSE, className = 'details-control', targets = 1) # turn first column into control column
)
),
callback = JS(callback)
) |
This breaks if a nested table also contains a nested table. No data is shown for such a row. Any ideas? |
Did you convert the version in the mtcars_nested_datatable.R.txt or the latest version I posted from June 17th? The posted version from June 17th supports multiple levels of nesting ( 3 levels) assuming the last column of each row contains a nested table. If the cell of data is NULL (aka doesn't contain a nested table) then it doesn't render a arrow icon nor tries to format data into another nested table.. Note that the detail-control is not added to a row if it doesn't have a nested table in the last column. The rest is managed in JavaScript On any details-control click: Adds detail control to the child table's first column which should contain a ► arrow icon for rows that contain a nested table in the last column. |
Oh maaan 😬 That worked... Thanks. I don't think that particular layout will work nicely for my use-case in the end though. So I set up a question on SO for where I am currently stuck: https://stackoverflow.com/questions/46593607/datatable-with-nesting-child-rows-and-modal |
You can inject formatting for every single table depending on how you want it to look.. Floating / repeating headers, justification, etc.. I think these are my usual suspects for formatting..
|
Thanks again. I used your last example now and I think I more or less understand. It is working as designed. Is there an easy way to set a maximum number of sub-grouping? So that for my use-case I can limit it to one layer of grouping and the have another layer of nesting I want to look at using a modal. |
OK I managed to get the one level of grouping only by remove the lines in the java script.
|
Hi, Is there a way to get this to work with the 'FixedColumns' extensions? I find that when I do that the row ordering breaks if I look at it in the browser and it stops working if I do it just in R. Observe the following for a full example:
|
davelee1972, Just came across this and I've not been able to get your example from june 17th to work. When I launch it, the table immediately fails with the error: "DataTables warning: table id = DataTables_Table_0 - Error in I'm not sure what what is causing the error. |
I just copied and pasted the June 17th code into my rstudio and ran it. It still works in a chrome browser.. Running R version 3.3.3 |
What version of DT are you using? my versions are: |
R 3.3.3 on my PC I get the same error after upgrading just packages to the latest version. Not sure if i have time to debug this this week.. If I look at cyl_dt and cyl_dt[1]$mpg it has:
This should be correct.. Clicking on the first row should produce a child data table with nine records. It looks like it is somehow iterating through the data structure and maybe trying to add extra columns to the parent table of 3 records from the child record of 9 rows. |
Thanks Dave. I'll be working on it some myself. I will let you know if I solve it. |
Here's a partial fix. with one outstanding item. Headers and columns aren't aligning unless you resize the window.. I ended up getting rid of all the nested data tables and converted everything to JSON strings and then used Javascript to convert JSON strings into Objects.
|
@davlee1972 @Plum57 I had to roll back to DT 0.2 to get my app working again. I'm not sure what exactly broke the package, but my hunch is it may have to do with a change to the actual DataTables js itself. As evidenced in this screenshot, they seem to have changed their site and featured our culprit functionality on the homepage... If this bug is resolved or there's a workaround without having to convert everything to json first, please share! |
This may be an easy one for some on this thread (apparently not me). Making a very simple modification of @stanstrup's 2018-10-5 example (built off of @davlee1972's previous example), I tried adding an additional grouping variable using
This works partially, creating a 31 row nested table. There is a two row child table for mpg/cyc/disp 21/6/160 (the only duplicate in this grouping), and several 1 row child tables as needed. But for 12 of the rows where there would be only one row in the child table, child tables are just headers with no data. Anyone see the problem? |
@joelnc yes i have a smilar issue, Ive used @davlee1972 example and substituted my own data to get a grouping of a few columns The resulting picture below is the output, but as you can see AccountRefFullname column and fullamount column only has the headers. in the case of the COGS row, its even missing the fullamount column heading. These AccountRefFullname column and fullamount headings for the different financial categories (should have multiple should have multiple rows underneath them. i dont have much of a javascript background and was hoping if anyone might be able to help out. thanks |
@piyuw 's question is answered here: https://stackoverflow.com/a/56599838/1100107 |
Something like https://datatables.net/examples/api/row_details.html
http://stackoverflow.com/questions/23635552/shiny-datatable-with-child-rows-using-ajax
The text was updated successfully, but these errors were encountered: