添加单选按钮以选择Shiny中的DataTable行 [英] Adding radiobutton to select a DataTable row in Shiny

查看:225
本文介绍了添加单选按钮以选择Shiny中的DataTable行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加radiButtons以选择Data中的行,即所选的单选按钮应传递给输入。
我无法在DT中使用内置行选择。我确实需要使用单选按钮来选择行。
这就是需要的内容:

I need to add radiButtons to select rows in Data, i.e. the radiobutton choosen should be passed to input. I cannot use built in row selection in DT. I really need to use radiobuttons to select the row. This is what is wanted:

使用 https://yihui.shinyapps.io/DT-radio/ 我可以选择列。
Es:

Using https://yihui.shinyapps.io/DT-radio/ I am able to select COLUMNS. Es:

library(shiny)
library(DT)
shinyApp(
ui = fluidPage(
title = 'Radio buttons in a table',
DT::dataTableOutput('foo'),
verbatimTextOutput("test")
),
server = function(input, output, session) {
m = matrix(
  c(round(rnorm(24),1), rep(3,12)), nrow = 12, ncol = 3, byrow = F,
  dimnames = list(month.abb, LETTERS[1:3])
)
for (i in seq_len(nrow(m))) {
  m[i, 3] = sprintf(
    if_else(i == 1,
            '<input type="radio" name="%s" value="%s" checked="checked"/>',
            '<input type="radio" name="%s" value="%s"/>'),
    "C", month.abb[i]
  )
}
m=t(m)

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() {
      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$test <- renderPrint(str(input$C))
}
)

结果是:

天真地,我试图删除m = t(m)并将行更改为回调中的列,但这不起作用,因为示例中的回调函数正在添加

Naively, I tried to remove m=t(m) and changing rows to columns in the callback. This is not working because the callback function in the example is adding class and id to the last , which have no counterpart for column.

有什么想法吗?

推荐答案

一个肮脏的解决方法可能是将整个数据表与 C div 中> id和 shiny-input-radiogroup 类:

A "dirty" fix could be to wrap the whole datatable in a div with the C id and the shiny-input-radiogroup class:

shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    tags$div(id="C",class='shiny-input-radiogroup',DT::dataTableOutput('foo')),
    verbatimTextOutput("test")
  ),
  server = function(input, output, session) {
    m = matrix(
      c(round(rnorm(24),1), rep(3,12)), nrow = 12, ncol = 3, byrow = F,
      dimnames = list(month.abb, LETTERS[1:3])
    )
    for (i in seq_len(nrow(m))) {
      m[i, 3] = sprintf(
        if_else(i == 1,
                '<input type="radio" name="%s" value="%s" checked="checked"/>',
                '<input type="radio" name="%s" value="%s"/>'),
        "C", month.abb[i]
      )
    }
    m
    output$foo = DT::renderDataTable(
      m, escape = FALSE, selection = 'none', server = FALSE,
      options = list(dom = 't', paging = FALSE, ordering = FALSE)
    )
    output$test <- renderPrint(str(input$C))
  }
)

这篇关于添加单选按钮以选择Shiny中的DataTable行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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