How to use download handler for the download button created inside datatable ?

I have created download buttons inside each row of the datatable. The table is inside one of the shiny module. I used Shiny.setInputValue to create download button as suggested here. The question is, which id should be attached to output while using downloadHandler inside server ?. In the below example i showed questionable value as XXXXXXXXXXXX

library(shiny)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable


## module UI
test_data_table_ui  <- function(id){
  ns <- NS(id)
  tagList(
    DT::dataTableOutput(outputId = ns("my_data_table"))
  )
  
}
  
## module server
test_data_table_server <- function(input, output, session ){
  ns = session$ns
  
  myValue <- reactiveValues(check = '')
  
  shinyInput <- function(FUN, len, id, ns, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(ns(id), i), ...))
    }
    inputs
  }
  
  
  my_data_table <- reactive({
    tibble::tibble(
      Name = c('Dilbert', 'Alice', 'Wally', 'Ashok', 'Dogbert'),
      Motivation = c(62, 73, 3, 99, 52),
      Actions = shinyInput(downloadButton, 
                           5,
                           'button_',
                           ns = ns,
                           label = "Download",
                           onclick = sprintf("Shiny.setInputValue('%s', this.id)",ns("select_button"))
      )
    )
  })
  

  
  # output$XXXXXXXXXXXX <- downloadHandler(
  #   filename = function() {
  #     paste('data-', Sys.Date(), '.txt', sep='')
  #   },
  #   content = function(file) {
  #     readr::write_delim(x = mpg, path = file, delim = "\t")
  #   }
  # )
    

  observeEvent(input$select_button, {
    print(input$select_button)
  })
  
  
  output$my_data_table <- DT::renderDataTable({
    return(my_data_table())
  }, escape = FALSE)
  
  
  
}


ui <- fluidPage(
  test_data_table_ui(id = "test_dt_inside_module")
)

server <- function(input, output, session) {
  callModule(module = test_data_table_server , id = "test_dt_inside_module")
}

shinyApp(ui, server)
#> 
#> Listening on http://127.0.0.1:3059

Created on 2019-09-17 by the reprex package (v0.3.0)

Same problem has also been posted on stack overflow and it is answered there.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.