Home > Net >  Using Shiny Alert with Download option
Using Shiny Alert with Download option

Time:12-17

I have a shiny app where the user can select the data of interest and it renders a table. The user can then click download in which I want a shinyalert() to appear and ask if they have received permission to download the data. Following them clicking yes I want the data to download. The code below shows the alert working properly but does not download the data.

Server

library(shiny)
library(shinyalert)

data <- data.frame(x=rnorm(10),y=rnorm(10))

# Define server logic required to draw a histogram
shinyServer(function(input, output) {


  
  # render data selection
  output$cap_table <- DT::renderDataTable(data)

    
    
    observeEvent(input$cap_download, {
      
      shinyalert(title = "Pump the breaks!", text = "Did you get approval for data use from the data owners?",
                 type = "warning", closeOnClickOutside = T, showCancelButton = T, inputId = "cap_dwnld",
                 showConfirmButton = T, confirmButtonText = "Yes", cancelButtonText = "No")
      
      output$cap_dwnld <- downloadHandler(
        filename = function(){"insert_name.csv"},
        content = function(fname){
          write.csv(cap_data(), fname)
          
        })
      
    })

})

UI

library(shiny)
library(shinyalert)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins
    sidebarLayout(
        sidebarPanel(
            
        ),

        # Show a plot of the generated distribution
        mainPanel(
          DT::dataTableOutput("cap_table"),
            useShinyalert(),
            actionButton('cap_download',"Download the data")
        )
    )
))

CodePudding user response:

I don't know of any way to make the downloadhandler work without the download button, but you can have it call a separate modal with the download button if the user clicks ok like this:

library(shiny)
library(shinyalert)

data <- data.frame(x=rnorm(10),y=rnorm(10))

# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  
  
  
  # render data selection
  output$cap_table <- DT::renderDataTable(data)
  
  
  
  observeEvent(input$cap_download, {
    
    shinyalert(title = "Pump the breaks!", text = "Did you get approval for data use from the data owners?",
               type = "warning", closeOnClickOutside = T, showCancelButton = T, inputId = "cap_download_btn",
               showConfirmButton = T, confirmButtonText = "Yes", cancelButtonText = "No")
  })
  
  observeEvent(input$cap_download_btn,{
    if(input$cap_download_btn == T)
    showModal(modalDialog(downloadButton("cap_dwnld", "download"), footer = NULL, easyClose = T, size = "s"))
  })
  
  output$cap_dwnld <- downloadHandler(
    filename = function(){"insert_name.csv"},
    content = function(fname){
      removeModal()
      write.csv(data, fname)
    })
  
})

CodePudding user response:

With PapaParse, a Shiny message handler, and an alert callback:

library(shiny)
library(shinyalert)
library(jsonlite)

js <- '
$(document).ready(function() {
  Shiny.addCustomMessageHandler("json2csv", function(json) {
    csv = Papa.unparse(json);
    var a = document.createElement("a");
    a.href = "data:text/csv;charset=utf8,"   encodeURI(csv);
    a.target = "_blank";
    a.download = "mydata.csv";
    a.click();
  });
});'

data <- data.frame(x = rnorm(10), y = rnorm(10))

ui <- fluidPage(
  
  tags$head(
    tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.3.2/papaparse.min.js"),
    tags$script(HTML(js))
  ),

  titlePanel("Old Faithful Geyser Data"),

  sidebarLayout(
    sidebarPanel(),

    mainPanel(
      DT::dataTableOutput("cap_table"),
      actionButton("cap_download", "Download the data")
    )
  )
)

server <- function(input, output, session) {
  # render data selection
  output$cap_table <- DT::renderDataTable(data)

  observeEvent(input$cap_download, {
    shinyalert(
      title = "Pump the breaks!", 
      text = "Did you get approval for data use from the data owners?",
      type = "warning", 
      closeOnClickOutside = T, 
      showCancelButton = T, 
      inputId = "cap_dwnld",
      showConfirmButton = T, 
      confirmButtonText = "Yes", 
      cancelButtonText = "No",
      callbackR = function() {
        session$sendCustomMessage("json2csv", toJSON(data))
      }
    )
  })
}

shinyApp(ui, server)
  • Related