简体   繁体   English

添加radiobutton以选择Shiny中的DataTable行

[英]Adding radiobutton to select a DataTable row in Shiny

I need to add radiButtons to select rows in Data, ie the radiobutton choosen should be passed to input. 我需要添加radiButtons来选择Data中的行,即radiobutton选择应该传递给输入。 I cannot use built in row selection in DT. 我不能在DT中使用内置行选择。 I really need to use radiobuttons to select the row. 我真的需要使用radiobuttons来选择行。 This is what is wanted: 这就是你想要的: 想要行选择的结果

Using https://yihui.shinyapps.io/DT-radio/ I am able to select COLUMNS. 使用https://yihui.shinyapps.io/DT-radio/我可以选择COLUMNS。 Es: 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))
}
)

The result is: 结果是: 列选择的部分结果

Naively, I tried to remove m=t(m) and changing rows to columns in the callback. 天真地,我试图删除m = t(m)并将行更改为回调中的列。 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. 这不起作用,因为示例中的回调函数将class和id添加到last,而没有列的对应项。

Any idea? 任何的想法?

A "dirty" fix could be to wrap the whole datatable in a div with the C id and the shiny-input-radiogroup class: “脏”修复可能是将整个数据表用C id和shiny-input-radiogroup类包装在div

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM