R Shiny selectedInput inside renderDataTable 细胞

R Shiny selectedInput inside renderDataTable cells

我正在寻找将 selectedInputs 放入 renderDataTable 单元格的解决方案。我找到了 js 解决方案: https://datatables.net/examples/api/form.html ,但是我不知道如何在 shinyjs 中将此解决方案实现为 renderDataTable 对象。对于如何在 shiny 中实现可编辑的 renderDataTable 的提示/想法/解决方案,我将不胜感激。

为什么不用DT的标准函数(Shiny.bindAll)

示例(在第一行的控制台打印 select)

library(shiny)
library(DT)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = fluidPage(

      DT::dataTableOutput("mytable")
    )

  , server = function(input, output, session) {

    observe({
      print(input$row1)
    })

    output$mytable = DT::renderDataTable({
      #Display table with select
      DT::datatable(cbind(Pick=paste0('
                                      <select id="row', mymtcars$id, '"> <option>1</option>
                                      <option>2</option></select>',""), mymtcars),
                    options = list(orderClasses = TRUE,
                                   lengthMenu = c(5, 25, 50),
                                   pageLength = 25 ,

                                   drawCallback= JS(
                                     'function(settings) {
                                     Shiny.bindAll(this.api().table().node());}')
                                   ),selection='none',escape=F)


    } )

  })
)

与此非常相似:

library(shiny)
library(DT) 
runApp(list(
  ui = basicPage(
    h2('The mtcars data'),
    DT::dataTableOutput('mytable'),
    h2("Selected"),
    tableOutput("checked")
  ),

  server = function(input, output) {
    # helper function for making checkbox
    shinyInput = function(FUN, len, id, ...) { 
      inputs = character(len) 
      for (i in seq_len(len)) { 
        inputs[i] = as.character(FUN(paste0(id, i), label = NULL, ...)) 
      } 
      inputs 
    } 
    # datatable with checkbox
    output$mytable = DT::renderDataTable({
      data.frame(mtcars,Rating=shinyInput(selectInput,nrow(mtcars),"selecter_",
                                            choices=1:5, width="60px"))
    }, selection='none',server = FALSE, escape = FALSE, options = list( 
      paging=TRUE,
      preDrawCallback = JS('function() { 
Shiny.unbindAll(this.api().table().node()); }'), 
      drawCallback = JS('function() { 
Shiny.bindAll(this.api().table().node()); } ') 
    ) )
    # helper function for reading checkbox
    shinyValue = function(id, len) { 
      unlist(lapply(seq_len(len), function(i) { 
        value = input[[paste0(id, i)]] 
        if (is.null(value)) NA else value 
      })) 
    } 
    # output read checkboxes
    output$checked <- renderTable({
      data.frame(selected=shinyValue("selecter_",nrow(mtcars)))
    })
  }
))

请注意,如果您重新呈现 table,除非您添加一些额外的代码以取消绑定,否则输入将不起作用。

编辑:

假设 table 中的数据是反应性的,因此它会发生变化,并且 table 会重新呈现。您需要按照@yihui 此处明确取消绑定:https://groups.google.com/forum/#!msg/shiny-discuss/ZUMBGGl1sss/zfcG9c6MBAAJ

所以你需要在UI中添加:

tags$script(HTML("Shiny.addCustomMessageHandler('unbind-DT', function(id) {
          Shiny.unbindAll($('#'+id).find('table').DataTable().table().node());
        })"))

然后在服务器中,只要您重新呈现数据table,就会触发该函数:

session$sendCustomMessage('unbind-DT', 'mytable')

colnames 参数是一个列名向量,因此当您指定一个长度为 FALSE 的向量时,它会为您提供一个 table,其中一个列名为 FALSE。我不确定从 datatables 中删除列名的直接方法。这本身就是一个很好的 SO 问题。