TheComeOnMan TheComeOnMan - 15 days ago 14
R Question

Radio Buttons on Shiny Datatable, with data.frame / data.table

Pretty much a copy paste from this example https://yihui.shinyapps.io/DT-radio/ ( which, I assume, supersedes some of the other answers on SO ) except that I'm trying to use a data.table instead of a matrix. I'm unable to figure out why it isn't working.

library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput('sel')
),
server = function(input, output, session) {

m = data.table(
month1 = month.abb,
A = '1',
B = '2',
C = '3',
QWE = runif(12)
)
m[, A := 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]]))
})
}
)

Answer

Issue is with rownames. You have an extra column of rownames that gets all the shiny attributes added to it, but it isn't a radio buttons it's just text so it breaks (although it should throw an error).

Here is a working version:

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {

    m = data.table(
      month1 = month.abb,
      A = '1',
      B = '2',
      C = '3',
      QWE = runif(12)
    )
    m[, A := 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