【问题标题】:Get Selected Row From DataTable in Shiny App在 Shiny App 中从 DataTable 中获取选定的行
【发布时间】:2015-02-02 09:32:30
【问题描述】:

我想修改这个应用程序:

https://demo.shinyapps.io/029-row-selection/

这样一次只能选择一行,这样我就可以获取所选行第一列中的项目来绘制数据。有人知道怎么做吗?

【问题讨论】:

标签: r shiny


【解决方案1】:

更新:您现在可以在server.R 中使用input$tableId_rows_selected 访问选定的行。详情请见here

要选择唯一的行,您可以将示例的回调函数更改为:

callback = "function(table) {
      table.on('click.dt', 'tr', function() {
            table.$('tr.selected').removeClass('selected');
            $(this).toggleClass('selected');            
        Shiny.onInputChange('rows',
                            table.rows('.selected').data()[0][0]);
      });
    }"

当您单击一行时,它基本上会删除所有选定的行(它们具有 .selected 类)并选择您单击的行。

我还更改了 Shiny.onInputChange 函数中的代码,使其返回第一列中的数字。

【讨论】:

  • 如何修改此代码以从已排序的数据表中选择多行?我试过.data()[0][0].toArray(),但它不起作用。
【解决方案2】:

渲染 DataTable 的 R 方法有一个定义选择模式的参数。例如:

output$table1 <-
  DT::renderDataTable(dataSet,
                      selection = 'single')

可能的值是('multiple' 是默认值):

  • 单人
  • 多个

更多参考可以看:http://rstudio.github.io/DT/shiny.html

编辑 04/14/2016

在我使用 single 选择模式的设置中存在问题。

这是我使用的版本:

> DT:::DataTablesVersion
[1] "1.10.7"
> packageVersion("DT")
[1] ‘0.1’

我面临的问题是,在视觉上你有一个单行选择,但是当你这样做时:

observeEvent(input$table1_rows_selected, {
  str(input$table1_rows_selected)
})

您将获得一个列表,其中包含所有已选择但未明确取消选择的行。换句话说,选择新行不会自动取消选择内部数据表逻辑中的前一行。这也可能是由于 DT 包装,不确定。

这就是为什么目前我们使用 JS 作为解决方法:

$(document).on('click', '#table1 table tr', function() {
    var selectedRowIds = $('#table1 .dataTables_scrollBody table.dataTable').DataTable().rows('.selected')[0];

    var selectedId = "";
    if (selectedRowIds.length === 1) {
        selectedId = $(this).children('td:eq(0)').text();
    } else {
      $('#table1 tbody tr').removeClass('selected');
    }
    Shiny.onInputChange("table1_selected_id", selectedId);
});

一旦你有了这个,你就可以做到:

observeEvent(input$table1_selected_id, {
  str(input$table1_selected_id)
})

现在这至少会向您的 server.R 代码发送正确的数据。不幸的是,您仍然会遇到表格问题,因为在内部它会跟踪选择了哪些行,如果您切换页面,它将恢复错误的选择。但至少这纯粹是一个视觉缺陷,您的代码将有机会正常运行。所以这个解决方案实际上需要更多的工作。

【讨论】:

  • 这在今天的 CRAN 版本中仍然是一个问题,但它似乎在开发版本中得到了修复:devtools::install_github('rstudio/DT')
  • 如果你知道你在处理单选,你可以使用_row_last_clicked
【解决方案3】:

以下代码以 DT 表格式显示数据框。用户将能够选择单行。检索并显示选定的行。您可以在服务器的绘图块中编写绘图功能。

希望对你有帮助!!

         # Server.R
         shinyServer(function(input, output,session) {




          output$sampletable <- DT::renderDataTable({
          sampletable
          }, server = TRUE,selection = 'single')  

          output$selectedrow <- DT::renderDataTable({

          selectedrowindex <<-     input$sampletable_rows_selected[length(input$sampletable_rows_selected)]
         selectedrowindex <<- as.numeric(selectedrowindex)
         selectedrow <- (sampletable[selectedrowindex,])
         selectedrow



           })

          output$plots <- renderPlot({

          variable <- sampletable[selectedrowindex,1]
          #write your plot function


              })


          })

          #ui.R 
          shinyUI(navbarPage( "Single Row Selection",



                tabPanel("Row selection example",
                         sidebarLayout(sidebarPanel("Parameters"),
                             mainPanel(
                               DT::dataTableOutput("selectedrow"),   
                             DT::dataTableOutput("sampletable")

                           ))

                      )

                      ))

         # global.R 

        library(DT)
        library(shiny)
        selectedrowindex = 0

【讨论】:

    【解决方案4】:

    你可以用这个:

    output$data_table <- renderDataTable(data(),options = list(pageLength = 9))
    

    然后获取选中的行:

    selected <- input$data_table_rows_selected
    

    然后要从该行获取单元格,您可以使用(假设时间是您在这种情况下尝试获取的列的名称):

    time_x = data()[selected, "time"]
    

    Selected 是所选行的索引,因此您需要将该索引与列名一起使用。

    【讨论】:

      猜你喜欢
      • 2017-11-29
      • 2018-05-30
      • 2017-02-20
      • 2017-12-11
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多