shiny allowling users to choose which columns to display

后端 未结 2 948
名媛妹妹
名媛妹妹 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:34

    Here is an example. It uses selectInput to select columns, and displays all columns by default until you select one or more specific columns.

    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]
        })
      }
    ))
    

提交回复
热议问题