2016-04-22 51 views
0

我涉足閃亮的數據表特性,我有興趣創建一個列出數據表的所有列的井式面板或側面板,並允許用戶選擇他們想要的列請參閱數據表。閃亮的允許用戶選擇要顯示的列

眼下這個代碼下圖顯示玩具數據集的所有列mtcars

library(shiny) 

runApp(list(
    ui = basicPage(
    h2('The mtcars data'), 
    dataTableOutput('mytable') 
), 
    server = function(input, output) { 
    output$mytable = renderDataTable({ 
     mtcars 
    }) 
    } 
)) 

我感興趣的提供用戶把這些列無論是在能力或關閉使用複選框

[1] "mpg" "cyl" "disp" "hp" "drat" 
    [6] "wt" "qsec" "vs" "am" "gear" 
    [11] "carb" 

解決這個問題的任何幫助都非常有用。提前致謝。

回答

1

我的示例使用checkboxGroupInput選擇多列

library(shiny) 

vchoices <- 1:ncol(mtcars) 
names(vchoices) <- names(mtcars) 

runApp(list(
    ui = basicPage(
    h2('The mtcars data'), 
    checkboxGroupInput("columns","Select Columns",choices=vchoices,inline = T), 
    dataTableOutput('mytable') 


), 
    server = function(input, output) { 

    observeEvent(input$columns,{ 
     cols <- as.numeric(input$columns) 
     if(length(input$columns) == 1){ 
     df <- data.frame(mtcars[,cols]) 
     names(df) <- names(mtcars)[cols] 
     output$mytable = renderDataTable(df) 

     }else{ 
     output$mytable = renderDataTable(mtcars[,cols]) 

     } 


    }) 

    } 
)) 
+0

我喜歡你的方法,我打算在頂部添加一個下載按鈕,以便用戶可以下載過濾的數據表。如果你的代碼列出了頂部那樣的列,恐怕不會有任何下載選項的空間,或者對齊可能會被搞砸。 –

+0

佈局上閃亮的文檔可能是您應該查看的內容。 [鏈接](http://shiny.rstudio.com/articles/layout-guide.html) –

+0

這是一個很好的,謝謝亞當 –

3

這裏是一個例子。它使用selectInput來選擇列,並默認顯示所有列,直到您選擇一個或多個特定列。

library(shiny) 
runApp(list(
    ui = basicPage(
    selectInput("select", "Select columns to display", names(mtcars), multiple = TRUE), 
    h2('The mtcars data'), 
    dataTableOutput('mytable') 
), 
    server = function(input, output) { 
    output$mytable = renderDataTable({ 
     columns = names(mtcars) 
     if (!is.null(input$select)) { 
     columns = input$select 
     } 
     mtcars[,columns,drop=FALSE] 
    }) 
    } 
)) 
+0

這看上去很聰明。 –