mercredi 2 août 2023

Add a column of checkboxes to a DT table

Building on an earlier question that StackOverflow came to the rescue on... I'm building an app that connects to a database and pulls in a bunch of data, then lets the user "drill down" through a series of selectInput's and then ultimately allows them to download the results a .csv. After the previous question, I can now select which columns are shown in the datatable, as shown in this toy example.

library(shiny)
library(DT)
library(magrittr)

ids <- c(1, 2, 3, 4, 5)
firstNames <- c("Bob", "Jane", "Jim", "Billy", "Siouxsie")
lastNames <- c("Smith", "Jones", "Thomas", "Idol", "Sioux")
FaveColors <- c("Blue", "Green", "Yellow", "Red", "Black")
df <- data.frame(ids, firstNames, lastNames, FaveColors)

# Define UI 
ui <- fluidPage(

    # Application title
    titlePanel("Minimal Example"),

    # Sidebar 
    sidebarLayout(
        sidebarPanel(
            tabPanel("Columns",
                      checkboxGroupInput(inputId = "ColumnsToShow", label = "Output Columns",
                                         choices = names(df)
                      )
            )
        ),

        # Show a table
        mainPanel(
          DT::dataTableOutput("FilteredDataFrame")
        )
    )
)

# Define server logic 
server <- function(input, output) {
  
  filtered_df <- reactive({
    temp_frame <- df %>% select(all_of(input$ColumnsToShow))
    return(temp_frame)
  })

  output$FilteredDataFrame <- DT::renderDT(server=TRUE, {datatable(filtered_df(), extensions = 'Buttons',
                                                                   options = list(scrollx=TRUE,
                                                                                  lengthMenu = c(10,20,30),
                                                                                  paging = TRUE,
                                                                                  searching = TRUE,
                                                                                  fixedColumns = TRUE,
                                                                                  autoWidth = TRUE,
                                                                                  ordering = TRUE,
                                                                                  #dom = 'Bfrtip',
                                                                                  dom = 'tlip',
                                                                                  buttons = c('copy', 
                                                                                              'csv',
                                                                                              'excel')
                                                                   )
  )
  }
  )
}

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

Now the user has asked me to add a column at the start of the table that contains checkboxes. They would use these to flag entries that may have an error in the data. So ultimately I would gather the checked rows and build an SQL query to push back to a different database creating records for rows that need to be checked.

I've been looking around and most of the solutions I see involve some fairly hefty (for me anyway) javascript programming. Or I also found the shinydust package that has a built in data table with a column of checkboxes, but seems to lack the nice pagination and presentation of DT.

Any pointers on how to achieve this would be most appreciated.




Aucun commentaire:

Enregistrer un commentaire