2016-11-23 35 views
2

有没有办法将datatable筛选到选定的行?筛选数据表只列出选定的行

我有一个巨大的数据框20000行,这是有点棘手,如果你搜索并选择一些行。如果您想取消选择它们,您必须浏览列表并搜索已经点击的行或重置您的完整选择。

我认为这将是很好的过滤表只有选定的行和用户可以取消选择这些。

library(shiny) 
library(DT) 

ui <- shinyUI(
    fluidPage(
     DT::dataTableOutput("name_table") 
) 
) 

server <- function(input, output, session) { 
    output$name_table <- DT::renderDataTable({ 
    DT::datatable(mtcars, 
        options=list(pageLength=5), 
        selection=list(selected=c(1,3,32))) 
    }) 
    name_proxy = DT::dataTableProxy('name_table') 
} 

shinyApp(ui, server) 

因此,在我的例子中,它应该过滤列表1,3和32行,所有三个应该被选中,所以我可以取消选择它们。

我希望我很清楚自己想做什么。

回答

0

我不知道是否有可能只有一个表,但通过创建第二个表,其中包含所有选定的条目,并允许第一个表中的选择根据您(非)选择的内容进行更新第二张桌子,你会得到想要的效果。 由于表的反应性导致问题,我在选择的更新周围添加了actionButton,但我认为没有它应该是可行的。

当前第二个表总是显示在第一个表中选中的所有行被选中的内容。如果要修剪你的第二个表取消选择行的选择,然后按Update selections按钮

library(shiny) 
library(DT) 

ui <- shinyUI(
    fluidPage(
    DT::dataTableOutput("name_table"), 
    DT::dataTableOutput("selected_table"), 
    actionButton("selectbutton", label = "Update selections") 
) 
) 

server <- function(input, output, session) { 
    mtcarsmod <- mtcars # We need a table with the row numbers as a column 
    mtcarsmod$Rownr <- 1:nrow(mtcars) 

    output$name_table <- DT::renderDataTable({ 
    DT::datatable(mtcarsmod, 
        options=list(pageLength=5), 
        # Have those rows selected that show up in the selected_table 
        selection= list(selected= 
            c(1,3,32) 
       ) 
    ) 
    }) 

    name_proxy <- DT::dataTableProxy('name_table') # Proxy to use for updating the selection 

    mtcars_selected <- reactive(mtcarsmod[input$name_table_rows_selected,]) # The selected options from the first table 

    output$selected_table <- DT::renderDataTable({ 
    try(DT::datatable(mtcars_selected(), 
         options=list(pageLength=5), 
         # have all entries selected 
         selection= list(selected= 
             1:nrow(mtcars_selected())) 
    )) 
    }) 


    observeEvent(input$selectbutton, # When you press the button update the selections in the first table 
       selectRows(name_proxy, mtcars_selected()[input$selected_table_rows_selected,"Rownr"]) 
) 

} 

shinyApp(ui, server) 
+0

我更寻找一个解决方案,以更新的方式显示的数据只显示选定的行...这两个表格版本的工作原理和我以前使用,但并不方便... – drmariod

+0

这只是一个显示不同的表格的问题。例如,如果您有一个带主表的选项卡和一个带选定表的选项卡,切换选项卡基本上“更新显示的数据以仅显示选定的行”,对吗?也许我不理解你想要实现的一部分。 –