闪亮的数据表上的单选按钮,带有data.frame/data.table [英] Radio Buttons on Shiny Datatable, with data.frame / data.table

查看:58
本文介绍了闪亮的数据表上的单选按钮,带有data.frame/data.table的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此示例中的副本粘贴了很多(我假设已取代了一些示例)关于SO的其他答案),除了我试图使用data.table而不是矩阵.我无法弄清楚为什么它不起作用.

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]]))
    })
  }
)

推荐答案

问题与行名有关.您还有一排额外的行名,可将所有闪亮的属性添加到其中,但这不是单选按钮,它只是文本,因此会中断(尽管应该抛出错误).

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).

这是一个有效的版本:

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]]))
    })
    }
)

这篇关于闪亮的数据表上的单选按钮,带有data.frame/data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆