Shiny Datatable上的单选按钮,带有data.frame / data.table

时间:2016-11-17 17:55:30

标签: r shiny data.table dt

几乎是来自this example的复制粘贴(我假设,取代SO上的其他一些答案),除了我正在尝试使用data.table而不是矩阵。我无法弄清楚它为什么不起作用。

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

1 个答案:

答案 0 :(得分:3)

问题与rownames有关。你有一个额外的rownames列,可以添加所有闪亮的属性,但它不是单选按钮,它只是文本,所以它会中断(尽管它会引发错误)。

这是一个工作版本:

 <dependencies>


    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>



    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.24</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.24</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-processing</artifactId>
        <version>2.24</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.24</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-sse</artifactId>
        <version>2.24</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.8</version>
        <type>jar</type>
    </dependency>
</dependencies>