在R中使用DT闪亮数据表将列选择移动到数据框的顶部

问题描述 投票:0回答:1

我想将列选择功能移至数据框的顶部而不是底部。我并不真正需要当前标头提供的排序功能,以便可以将其移动到底部或完全删除。这可能吗?

library(shiny)
library(DT)

mapping_list_headers <- mtcars

# Define UI
ui <- fluidPage(
  titlePanel("Remove Extra Headers and Columns from Dataframe"),
  mainPanel(
    helpText("Select the rows/columns to remove by clicking on them."),
    actionButton("removeRows", "Remove Selected Rows"),
    actionButton("removeCols", "Remove Selected Columns"),
    actionButton("closeApp", "Close App and Session"),
    hr(),
    fluidRow(
      column(12, DT::dataTableOutput("data_table"))
    )
  )
)


# Define server logic
server <- function(input, output, session) {
  # Sample dataframe
  data <- reactiveVal(mapping_list_headers)
  
  # Display dataframe in table format
  output$data_table <- renderDataTable({
    datatable(data(),selection = list(mode = 'multiple', target = 'row+column'))
  })
  
  # Remove selected rows
  observeEvent(input$removeRows, {
    if (!is.null(input$data_table_rows_selected)) {
      print(str(input))
      print((input))
      selected_rows <- input$data_table_rows_selected
      print(selected_rows)
      df_data <- data()
      df_data <- df_data[-selected_rows, , drop = FALSE]
      data(df_data)
    }
  })
  
  # Remove selected columns
  observeEvent(input$removeCols, {
    if (!is.null(input$data_table_columns_selected)) {
      selected_cols <- input$data_table_columns_selected
      print(selected_cols)
      df_data <- data()
      df_data <- df_data[, -selected_cols, drop = FALSE]
      data(df_data)
    }
  })
  
  # Close the app and session
  observeEvent(input$closeApp, {
    data_out <<- data()
    session$close()
    stopApp()
  })
}

# Run the application
shinyApp(ui = ui, server = server)

r shiny dt
1个回答
0
投票

试试这个

# Display dataframe in table format
  output$data_table <- renderDataTable({
    datatable(data(),
              selection = list(mode = 'multiple', target = 'row+column'),
              options = list(
                headerCallback = JS(
                  "function(thead, data, start, end, display){",
                  "  $(thead).remove();",
                  "}")
              )
              )
  })
© www.soinside.com 2019 - 2024. All rights reserved.