glin / reactable

Interactive data tables for R
https://glin.github.io/reactable
Other
627 stars 80 forks source link

JavaScript API in R Shiny application #364

Closed RoryChenXY closed 6 months ago

RoryChenXY commented 6 months ago

Can you please provide an example of using the JS API within R shiny app? Huge thanks!

Reactable.downloadDataCSV('cars-table', 'cars.csv')
library(shiny)

ui <- fluidPage(
  reactableOutput("cars_table")
)

server <- function(input, output) {
  output$cars_table <- renderReactable({
    reactable(MASS::Cars93)
  })
}

shinyApp(ui, server)
RoryChenXY commented 6 months ago
library(shiny)

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),
  )
}

ui <- fluidPage(
  csvDownloadButton("cars_table"),
  reactableOutput("cars_table")
)

server <- function(input, output) {
  output$cars_table <- renderReactable({
    reactable(MASS::Cars93)
  })
}

shinyApp(ui, server)