访问具有多页的 DataTable 中的单选按钮值 (Shiny)

Accessing Radio Button Values in DataTable with multiple page (Shiny)

提问人:loup piedfer 提问时间:11/14/2023 更新时间:11/15/2023 访问量:34

问:

当单选按钮值嵌入到带有该选项的闪亮数据表中时,我无法访问它们。我的代码基于这个例子: DT中的单选按钮paging = TRUE

这适用于第一页,但不适用于其他页面上的输入。

下面是示例的代码:

library(shiny)
library(DT)

shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {
    m = matrix(
      as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE,
      dimnames = list(month.abb, LETTERS[1:5])
    )
    for (i in seq_len(nrow(m))) {
      m[i, ] = sprintf(
        '<input type="radio" name="%s" value="%s"/>',
        month.abb[i], m[i, ]
      )
    }
    m
    output$foo = DT::renderDataTable(
      m, escape = FALSE, selection = 'none', server = FALSE,
      options = list(dom = 'Bfrtip', 
                     paging = TRUE, 
                     pageLength = 6,
                     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]]))
    })
  }
)

在此示例中,我无法访问“7 月”到“12 月”月份的单选按钮值。 事实上,我有一个超过 100 行的表,我需要访问各个页面的输入值。

javascript r 闪亮 的 dt 无线电组

评论


答:

1赞 YBS 11/15/2023 #1

在此处添加 @Stephane Laurent 给出的 for Shiny 和preDrawCallbackdrawCallback

 output$foo = DT::renderDataTable(
      m, escape = FALSE, selection = 'none', server = FALSE,
      options = list(dom = 'Bfrtip', 
                     paging = TRUE, 
                     pageLength = 6,
                     preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                     drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } '),
                     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());")
    )