r - shiny allowling users to choose which columns to display -
i dabbling datatable feature in shiny , interested in creating wellpanel or sidepanel lists columns of datatable , allows users choose columns want see on datatable.
right code below displays columns of toy dataset mtcars
library(shiny) runapp(list( ui = basicpage( h2('the mtcars data'), datatableoutput('mytable') ), server = function(input, output) { output$mytable = renderdatatable({ mtcars }) } ))
i interested in providing users ability turn these columns either on or off using checkbox
[1] "mpg" "cyl" "disp" "hp" "drat" [6] "wt" "qsec" "vs" "am" "gear" [11] "carb"
any on addressing issue appriciated. in advance.
my example uses checkboxgroupinput
select multiple columns
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]) } }) } ))
Comments
Post a Comment