Q:

How to use downloadHandler for the download button create inside shiny datatable?

I created download button inside each row of shiny datatable as shown here. The table is inside one of the shiny module. I wonder which id should be attached to output while using downloadHandler for any of the download button ? I have shown questionable value as XXXXXXXXXXXX in the code below. 
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)

A:

It works like this:
library(shiny)
library(DT)

## 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"))
      )
    )
  })

  lapply(1:5, function(i){
    output[[paste0("button_",i)]] <- downloadHandler(
      filename = function() {
        paste('data-', Sys.Date(), '.txt', sep='')
      },
      content = function(file) {
        readr::write_delim(x = iris, path = file, delim = "\t")
      }
    )
  })

  observeEvent(input$select_button, {
    print(input$select_button)
  })

  output$my_data_table <- DT::renderDataTable({
    datatable(my_data_table(), escape = FALSE, 
              options = 
                list(
                  preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
                  drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
                )
    )
  })
}

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)

