2016-04-22 59 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

这看上去很聪明。 –