【问题标题】:Shiny: Getting info from the selected row in a DT data tableShiny:从 DT 数据表中的选定行获取信息
【发布时间】:2015-06-29 22:39:32
【问题描述】:

我们正在尝试重新创建示例:https://demo.shinyapps.io/029-row-selection/ ,使用 DT 包而不是闪亮包来渲染数据帧。 DT::Datatable 也有 'callback' 选项,但在使用与演示中相同的 javascriptcode 时,它​​似乎不起作用。

我们当前的代码:

    shinyServer(function(input, output) {
      output$tbl <- DT::renderDataTable(
       DT::datatable(mtcars,options = list(pageLength = 10, 
        callback = JS("function(table) {
                                         table.on('click.dt', 'tr', function() {
                                         $(this).toggleClass('selected');
                                         Shiny.onInputChange('rows',
                                         table.rows('.selected').indexes     ().toArray());
                                         });
    }")
    ))
   )

    output$rows_out <- renderText({
      paste(c('You selected these rows on the page:', rows),
          collapse = ' ')
    })
  })

有谁知道如何做到这一点?

提前非常感谢, 托马斯

PS:在以下示例中,我们发现了如何为数据表中的列着色: renderDataTable Select all cells containing value > 10 and highlight

【问题讨论】:

    标签: r callback


    【解决方案1】:

    好的,我找到了解决方案。 这是有效的,清理输出需要做更多的工作,但我认为我们已经完成了 90%。 最大

    library(shiny)
    library(DT)
    shinyApp(
        ui = fluidPage(dataTableOutput('foo'),textOutput('rows_out')),
        server = function(input, output) {
            output$foo = renderDataTable({
                datatable(iris, 
                    callback = JS(
                        "table.on('click.dt', 'tr', function() {
                                                $(this).toggleClass('selected');
                            Shiny.onInputChange('rows',
                            table.rows('.selected').data().toArray());
                                            });")
                )
            })
            output$rows_out =renderText({
    
                paste(c('You selected these rows on the page:', input$rows),
                      collapse = ' ')
            })
        }
    
    )
    

    【讨论】:

      猜你喜欢
      • 2016-11-25
      • 1970-01-01
      • 2021-12-20
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 2017-11-29
      • 2018-06-22
      相关资源
      最近更新 更多