r - Radio Buttons on Shiny Datatable, with data.frame / data.table -
pretty copy paste this example (which, assume, supersedes of other answers on so) except i'm trying use data.table instead of matrix. i'm unable figure out why isn't working.
library(shiny) library(dt) shinyapp( ui = fluidpage( title = 'radio buttons in table', dt::datatableoutput('foo'), verbatimtextoutput('sel') ), server = function(input, output, session) { m = data.table( month1 = month.abb, = '1', b = '2', c = '3', qwe = runif(12) ) m[, := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, a] )] m[, b := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, b] )] m[, c := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, c] )] output$foo = dt::renderdatatable( m, escape = false, selection = 'none', server = false, options = list(dom = 't', paging = false, ordering = false), callback = js("table.rows().every(function(i, tab, row) { var $this = $(this.node()); $this.attr('id', this.data()[0]); $this.addclass('shiny-input-radiogroup'); }); shiny.unbindall(table.table().node()); shiny.bindall(table.table().node());") ) output$sel = renderprint({ str(sapply(month.abb, function(i) input[[i]])) }) } )
issue rownames. have column of rownames gets shiny attributes added it, isn't radio buttons it's text breaks (although should throw error).
here working version:
library(shiny) library(dt) shinyapp( ui = fluidpage( title = 'radio buttons in table', dt::datatableoutput('foo'), verbatimtextoutput('sel') ), server = function(input, output, session) { m = data.table( month1 = month.abb, = '1', b = '2', c = '3', qwe = runif(12) ) m[, := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, a] )] m[, b := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, b] )] m[, c := sprintf( '<input type="radio" name="%s" value="%s"/>', month1, m[, c] )] output$foo = dt::renderdatatable( m, escape = false, selection = 'none', server = false, rownames=false, options = list(dom = 't', paging = false, ordering = false), callback = js("table.rows().every(function(i, tab, row) { var $this = $(this.node()); $this.attr('id', this.data()[0]); $this.addclass('shiny-input-radiogroup'); }); shiny.unbindall(table.table().node()); shiny.bindall(table.table().node());") ) output$sel = renderprint({ str(sapply(month.abb, function(i) input[[i]])) }) } )
Comments
Post a Comment