Select & retrieve multiple rows from a sorted datatable in R Shiny -
using https://demo.shinyapps.io/029-row-selection/ reference, building app select number of rows in sorted/unsorted data & retrieve corresponding indices. if use code as is, not work on sorted data. changed code follows:
server.r
library(shiny) shinyserver(function(input, output) { output$tbl <- renderdatatable( mtcars, options = list(pagelength = 10), callback = "function(table) { table.on('click.dt', 'tr', function() { $(this).toggleclass('selected'); shiny.oninputchange('rows', table.rows('.selected').data()[0][0]); # returns actual row number not allow multiple selection }); }" ) output$rows_out <- rendertext({ paste(c('you selected these rows on page:', input$rows), collapse = ' ') }) })
ui.r
library(shiny) shinyui(fluidpage( title = 'row selection in datatables', sidebarlayout( sidebarpanel(textoutput('rows_out')), mainpanel(datatableoutput('tbl')), position = 'right' ) ))
so use shiny.oninputchange('rows',table.rows('selected').data()[0][0]);
when table sorted, returns row number in actual dataset , not index based on visible table. problem returns first selection.
i tried using shiny.oninputchange('rows',table.rows('selected').data()[0][0].toarray());
selected row numbers in array strangely doesn't return anything, not first selected row number (like above).
this problem has been solved in dt package. no longer need write javascript. please see http://rstudio.github.io/dt/shiny.html examples.
Comments
Post a Comment