闪亮:从DT数据表中的选定行获取信息

时间:2015-04-22 10:30:03

标签: r callback

我们正在尝试重新创建示例:https://demo.shinyapps.io/029-row-selection/ ,使用DT包渲染数据框而不是闪亮的包。 DT :: Datatable也有'回调'选项,但在使用与演示中相同的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

1 个答案:

答案 0 :(得分:5)

好的,我找到了解决方案。 这是有效的,清理输出需要更多的工作,但我认为我们完成了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 = ' ')
        })
    }

)