From 4c3bcd93fef4c467d21d7bb8e924dc3570e5b026 Mon Sep 17 00:00:00 2001 From: Nathan Hall Date: Mon, 9 Sep 2024 09:33:54 -0400 Subject: [PATCH] Fixing #302 and Filtered Data Downloads All filtered data downloads across all modules should work now, with a button styled the same as the full download. It needs to be a CSV handler though due to the reactable statte --- R/components-data-viewer.R | 69 +++++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 15 deletions(-) diff --git a/R/components-data-viewer.R b/R/components-data-viewer.R index 6f54453..21ae4ee 100644 --- a/R/components-data-viewer.R +++ b/R/components-data-viewer.R @@ -13,6 +13,24 @@ #' @family {Utils} #' @return shiny module UI #' @family {Utils} + +convertToDataFrame <- function(x, session, inputname) { + do.call("rbind.data.frame", x) +} + +registerInputHandler("to_csv", convertToDataFrame, force = TRUE) + +filteredDownloadButton <- function(tableId, label = "Download (Filtered)", filename = "filteredData.csv", icon = shiny::icon("download")) { + tags$div(class = "col-sm-3", + htmltools::tags$button( + class = "btn btn-default", + tags$i(class = "fas fa-download", role = "presentation", "aria-label" = "download icon"), + label, + onclick = sprintf("Reactable.downloadDataCSV('%s', '%s')", tableId, filename) + ) + ) +} + resultTableViewer <- function( id = "result-table", downloadedFileName = NULL, @@ -37,32 +55,41 @@ resultTableViewer <- function( icon = shiny::icon("download") ) ), + + # shiny::column( + # width = 3, + # shiny::downloadButton( + # ns('downloadDataFiltered'), + # label = "Download (Filtered)", + # icon = shiny::icon("download"), + # onClick = "Shiny.setInputValue('table_state:to_csv', + # Reactable.getState('resultData').sortedData)" + # ) + # ) + shiny::column( width = 3, - shiny::actionButton( - ns('downloadDataFiltered'), - label = "Download (Filtered)", - icon = shiny::icon("download"), - onclick = paste0( - "Reactable.downloadDataCSV('", - ns('resultData'), - "', 'result-data-filtered-", - downloadedFileName, - Sys.Date(), - ".csv')" + htmltools::browsable( + htmltools::tagList( + filteredDownloadButton("resultDataFiltered", "Download (Filtered)", + filename = paste('result-data-filtered-', downloadedFileName, Sys.Date(), '.csv', sep = ''), ) + ) ) ) ), + shiny::fluidRow( shinycssloaders::withSpinner( reactable::reactableOutput( outputId = ns("resultData"), - width = "100%") + width = "100%" ) - ) + ) + ) ) - )) + ) + ) } @@ -349,6 +376,8 @@ fuzzySearch<- reactable::JS('function(rows, columnIds, filterValue) { }); }') + + output$resultData <- reactable::renderReactable({ if (is.null(input$dataCols)) { data = newdf() @@ -388,7 +417,7 @@ fuzzySearch<- reactable::JS('function(rows, columnIds, filterValue) { rowStyle = list( height = height ), - elementId = elementIdName() + elementId = 'resultDataFiltered' #, experimental #theme = ohdsiReactableTheme ) @@ -396,6 +425,16 @@ fuzzySearch<- reactable::JS('function(rows, columnIds, filterValue) { # ) }) + output$downloadDataFiltered <- shiny::downloadHandler( + filename = function() { + paste("result-data-filtered-", Sys.Date(), ".csv", sep = "") + }, + content = function(file) { + data <- input$table_state + write.csv(data, file) + } + ) + # download full data button output$downloadDataFull <- shiny::downloadHandler(