glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
612 stars 79 forks source link

reactable Download CSV module only downloads the first level of data. #350

Open denizduvar opened 6 months ago

denizduvar commented 6 months ago

Hi everyone,

I'm dealing with a dataset that has more than one sublevel. The structure looks like this:

image

I'm using a module to download data as CSV in Reactable. However, this module only downloads the first level. Below, I'm providing the module and an example application that shows the same problem. I am open to your suggestions.

library(shiny)
library(reactable)
library(dplyr)

csvDownloadButton <- function(id, filename = "data.csv", label = "Download as CSV") {
  tags$button(
    # Button class
    class="btn btn-default action-button shiny-bound-input",
    # Adding icon and label
    tagList(icon("download"), label),
    # Download csv
    onclick = sprintf("Reactable.downloadDataCSV('%s', '%s')", id, filename),
  )
}

random_requests <- data.frame(
  Request_ID = c(1,2,3,4,5),
  Status = c("Accepted", "Accepted", "Accepted", "Declined", "Created")
)

random_samples <- data.frame(
  Request_ID = c(1,1,1,2,3,3,4,5),
  Sample_ID = c(1,2,3,4,5,6,7,8),
  statistics = sample(1:100, 8)
)

# Define UI for application 
ui <- fluidPage(

  selectInput(inputId = "select",
              label = "Select a choice:",
              choices = c("All", "Accepted", "Declined", "Created")),
  hr(),
  csvDownloadButton("table"),

  reactableOutput(outputId = "table")
)

# Define server logic 
server <- function(input, output) {

  data <- eventReactive(input$select, {
    if(input$select == "All"){
      random_requests
    } else {
      random_requests %>%
        filter(Status == input$select)
    }
  })

  output$table <- renderReactable({

    reactable(
      data(),
      details = function(index, name){
        request_id <- data()[index, "Request_ID"]
        htmltools::div(
          reactable(random_samples[random_samples$Request_ID == request_id, ])
        )
      }
    )
  })
}

# Run the application 
shinyApp(ui = ui, server = server)
glin commented 5 months ago

Hi, so the nested levels of data in this example are actually in completely separate tables, so they won't be included when downloading the CSV of the main table. I see two options here:

You can either use the built-in table grouping to display the nested data all in one table.

Or in each expandable details, you could possibly place another CSV download button to separately download the CSV for those nested tables.

denizduvar commented 4 months ago

Thanks for your reply to the question.