Shiny:从DT数据表中的所选行获取信息 [英] Shiny: Getting info from the selected row in a DT data table

查看:738
本文介绍了Shiny:从DT数据表中的所选行获取信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试重新创建示例: https://demo.shinyapps.io/029-行选择/
,使用DT包来渲染数据框而不是闪亮的包。 DT :: Datatable也有'callback'选项,但是在使用和演示相同的javascript代码时似乎不起作用。



我们当前的代码:

  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 );
});
})
))


输出$ rows_out< - renderText({
) c('您在页面上选择这些行:',行),
collapse ='')
})
})
pre>

有没有人知道如何实现这个?



/ p>

PS:在下面的示例中,我们找到了如何在一个数据表中对列进行着色:
renderDataTable选择所有包含值的单元格> 10并突出显示

解决方案


这是工作,需要更多的工作来清理输出,但我想我们有90%完成。
最大值

 库(闪亮)
库(DT)
shinyApp b 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());
});)

}
输出$ rows_out = renderText({

粘贴(c('您在页面上选择这些行:',输入$ rows),
collapse = ')
})
}



We are trying to recreate example: https://demo.shinyapps.io/029-row-selection/ , using the DT package for rendering the data frame instead of the shiny package. DT::Datatable also has the 'callback' option but it doesn't seem to work when using the same javascriptcode as in the demo.

our current code:

    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 = ' ')
    })
  })

Does anyone knows how to achieve this?

Many thanks in advance, Thomas

PS: In the following example we found how to color columns in a datatable: renderDataTable Select all cells containing value > 10 and highlight

解决方案

Ok I found the solution. This is working, few more work is needed to clean up the output, but I think we have 90% done. Max

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 = ' ')
        })
    }

)

这篇关于Shiny:从DT数据表中的所选行获取信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆