1

Pretty much a copy paste from this example (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]]))
    })
  }
)
Jaap
  • 81,064
  • 34
  • 182
  • 193
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54

1 Answers1

4

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]]))
    })
    }
)
Carl
  • 5,569
  • 6
  • 39
  • 74