Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
denizduvar opened this issue Dec 12, 2023 · 2 comments
Open

Comments

@denizduvar
Copy link

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
Copy link
Owner

glin commented Jan 29, 2024

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
Copy link
Author

Thanks for your reply to the question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants