shiny allowling users to choose which columns to display

后端 未结 2 932
名媛妹妹
名媛妹妹 2021-01-03 15:53

I am dabbling with the datatable feature in shiny and I am interested in creating a wellpanel or a sidepanel that lists all the columns of a datatable and allows users to ch

2条回答
  •  自闭症患者
    2021-01-03 16:17

    My example uses checkboxGroupInput to 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])
    
          }
    
    
        })
    
      }
    ))
    

提交回复
热议问题