2

I created downloadlinks inside rows of a reactable

I did this for a DT::datatable() and a reactable::reactable()

I also created the respective output downloadHandlers using apply on the ids

For the DT it works , but for the reactable it does not.
Please note that the trick for the DT to work as I found out here, is the:

   options = list(
          # see https://stackoverflow.com/a/57978298/8689932
          preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
          drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
        )

Can I do something similar for the reactable?

Related to this, and this

library(shiny)
library(reactable)
library(DT)

mtcars2 <- head(mtcars[1:3], 6)

ui <- fluidPage(
  
  helpText("The buttons in this DT table work"),
  DTOutput("tbl_DT"),
  hr(),
  helpText("The buttons in this reactable table do work"),
  reactableOutput("tbl_reactable"),
  
)

server <- function(input, output, session) {
  
  rv <- reactiveValues(
    downLinks = NULL
  )
  
  observe({
    
    ids <- seq_len(nrow(mtcars2))
    
    rv$downLinks <- sprintf('<a id="down_%s" class="shiny-download-link" href="" target="_blank" download>Download</a>', ids)
    
    # Create Outputs
    lapply(ids, function(id){
      
      output[[paste0("down_", id)]] <- downloadHandler(
        
        filename = function(){
          
          paste0("mtcars-from-button-", id, ".csv")
          
        },
        
        content = function(file){
          
          write.csv(mtcars2, file)
          
        }
      )
    })
    
    
  })
  
  output$tbl_reactable <- renderReactable({
    
      tbl <- mtcars2
      
      tbl$links <- rv$downLinks
      
      reactable(tbl,
        
        columns = list(
          links = colDef(html = TRUE)
        )
      )
    
    
  })
  
  
  
  output$tbl_DT <- renderDT({
    
    tbl <- mtcars2
    
    tbl$links <- rv$downLinks
    
      datatable(tbl,
        escape = FALSE, selection = "none",
        options = list(
          # This is important for the DownloadLink s towork
          # see https://stackoverflow.com/a/57978298/8689932
          preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
          drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
        )
      )
  })
  
}

shinyApp(ui, server)


Lefkios Paikousis
  • 462
  • 1
  • 6
  • 12

0 Answers0