2016-11-23 33 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

這只是一個顯示不同的表格的問題。例如,如果您有一個帶主表的選項卡和一個帶選定表的選項卡,切換選項卡基本上「更新顯示的數據以僅顯示選定的行」,對嗎?也許我不理解你想要實現的一部分。 –