【问题标题】:Accessing Radio Button Values in DataTable (Shiny)访问 DataTable 中的单选按钮值(闪亮)
【发布时间】:2021-05-16 15:15:24
【问题描述】:

当单选按钮输入值嵌入到闪亮的 DataTable 中时,我无法访问它们。我的代码基于此示例:Radio Buttons in DT

它访问第一次传递的值并将它们显示在下面,如以下屏幕截图所示:
First pass screenshot

一旦我更改了响应式重新渲染表格的输入,单选按钮中的值将不再显示,而是停留在第一次传递值上。这可以在以下屏幕截图中看到。 Second pass screenshot

第一次通过后,输入不再更新以反映单选按钮的选择。原始输入似乎冻结了,我找不到访问当前按钮值的方法。

提前感谢您的帮助!

这是上面例子的代码:

library(shiny)
library(DT)
library(data.table)
shinyApp(
  ui = fluidPage(
    title = 'Radio buttons in a table',
    selectInput('questions','Pick',c(1,2)),
    DT::dataTableOutput('foo'),
    verbatimTextOutput('sel')
  ),
  server = function(input, output, session) {
    
    myData = reactiveValues(m = NULL)
    temp = month.abb
    
    observeEvent(input$questions,{
      m = data.table(
        
        
        month1 = temp[1:5],
        A = '1',
        B = '2',
        C = '3',
        QWE = runif(5)
      )
      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]
      )]
      
      myData$m = m
    })
    
    
    
    output$foo = DT::renderDataTable({
      m = myData$m
      print(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]]))
    })
    }
)

【问题讨论】:

    标签: javascript r shiny radio-button dt


    【解决方案1】:

    那是因为您在observeEvent() 中重置m。如果您在观察者之外定义它,它会按预期工作。试试这个

      server = function(input, output, session) {
        
        myData <- reactiveValues(m = NULL)
        temp <- month.abb
        m <- data.table(
          month1 = temp[1:5],
          A = '1',
          B = '2',
          C = '3',
          QWE = runif(5)
        )
        
        observeEvent(input$questions,{
         
          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]
          )]
          
          myData$m = m
        })
        
        
        
        output$foo = DT::renderDataTable({
          m = myData$m
          print(m)
          
        }
        , escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE, # editable=TRUE,
        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]]))
        })
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 2019-02-10
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 2021-05-01
      • 2020-11-10
      • 1970-01-01
      相关资源
      最近更新 更多