1

I have seen many posts downloading files in IE using JavaScript, however as my knowledge about JavaScript is very small i would appreciate your help. Below we have a shiny app, which works perfectly in Firefox but while trying it in IE, download window does not appear at all.

library(shiny)
library(plotly)
library(lubridate)

d <- data.frame(X1 = rnorm(50,mean=50,sd=10), 
                X2 = rnorm(50,mean=5,sd=1.5), 
                Y = rnorm(50,mean=200,sd=25))

ui <-fluidPage(
  title = 'Download Plotly',
  sidebarLayout(

    sidebarPanel(
      htmlOutput("text"),
      selectInput("plot_download", "Select plot to download", choices=list("plot1","plot2")),
      actionButton('download_plot', "Download")

    ),

    mainPanel(

      plotlyOutput('regPlot'),
      plotlyOutput('regPlot2'),
      tags$script('

                  document.getElementById("download_plot").onclick = function() {
                  var plot = $("#plot_download").val();
                  if(plot == "plot1"){
                  var gd = document.getElementById("regPlot");
                  }else{
                  var gd = document.getElementById("regPlot2");
                  }
                  Plotly.Snapshot.toImage(gd, {format: "png"}).once("success", function(url) {
                  var a = window.document.createElement("a");
                  a.href = url; 
                  a.type = "image/png";
                  a.download = "plot.png";
                  document.body.appendChild(a);
                  a.click();
                  document.body.removeChild(a);                      
                  });
                  }
                  ')
    )
  )
)

server <- function(input, output, session) {

  output$text <- renderUI({
    eopm <- Sys.Date() - days(day(Sys.Date()))
    HTML(paste(eopm))
  })

  regPlot11 <- reactive({
    plot_ly(d, x = d$X1, y = d$X2, mode = "markers")
  })
  output$regPlot <- renderPlotly({
    regPlot11()
  })

  regPlot222 <- reactive({
    plot_ly(d, x = d$X1, y = d$X2, mode = "markers")
  })
  output$regPlot2 <- renderPlotly({
    regPlot222()
  })

}

shinyApp(ui = ui, server = server)

How can i make it work in IE?

[EDIT]

After a comments under my post i managed to get this far with help of navigator.msSaveBlob:

library(shiny)
library(plotly)
library(lubridate)

d <- data.frame(X1 = rnorm(50,mean=50,sd=10), 
                X2 = rnorm(50,mean=5,sd=1.5), 
                Y = rnorm(50,mean=200,sd=25))

ui <-fluidPage(
  title = 'Download Plotly',
  sidebarLayout(

    sidebarPanel(
      htmlOutput("text"),
      selectInput("plot_download", "Select plot to download", choices=list("plot1","plot2")),
      actionButton('download_plot', "Download")),

    mainPanel(

      plotlyOutput('regPlot'),
      plotlyOutput('regPlot2'),
      tags$script('document.getElementById("download_plot").onclick = function() {
                  var plot = $("#plot_download").val();
                  if(plot == "plot1"){
                  var gd = document.getElementById("regPlot");
                  }else{
                  var gd = document.getElementById("regPlot2");
                  }
                  if (navigator.msSaveBlob) { 
                   Plotly.Snapshot.toImage(gd, {format: "png"}).once("success", function(url){
                      var blob = new Blob([url],{type:"image/png"});
                      navigator.msSaveBlob(blob, "plot.png");
                  } else {
                      Plotly.Snapshot.toImage(gd, {format: "png"}).once("success", function(url) {
                      var a = window.document.createElement("a");
                      a.href = url; 
                      a.type = "image/png";
                      a.download = "plot.png";
                      document.body.appendChild(a);
                      a.click();
                      document.body.removeChild(a);                      
                      });
                  }

                  }
                  '))))

server <- function(input, output, session) {

  output$text <- renderUI({
    eopm <- Sys.Date() - days(day(Sys.Date()))
    HTML(paste(eopm))
  })

  regPlot1 <- reactive({
    plot_ly(d, x = d$X1, y = d$X2, mode = "markers")
  })
  output$regPlot <- renderPlotly({
    regPlot1()
  })

  regPlot2 <- reactive({
    plot_ly(d, x = d$X1, y = d$X2, mode = "markers")
  })
  output$regPlot2 <- renderPlotly({
    regPlot2()
  })

}

shinyApp(ui = ui, server = server)

However i am not able to retrieve object from Plotly.Snapshot.toImage function, i thought it is an url but as you can see in my code, it does not work... I would appreciate any help or tips!

Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • I removed the JS tag: the above code has nothing to do with JS, even if it eventually compiles down to JS somewhere – Matthew Herbst Feb 20 '19 at 11:58
  • 5
    See https://stackoverflow.com/questions/18394871/download-attribute-on-a-tag-not-working-in-ie – Stéphane Laurent Feb 20 '19 at 12:20
  • 3
    @MatthewHerbst This is the Javascript script which causes the issue. – Stéphane Laurent Feb 20 '19 at 12:23
  • Can you please try to check the console to see if there is any error or warning message. If you see that there is any error than let us know on which line you got that error. Also try to provide documentation for your Shiny code. Try to put a break point in developer tools and try to debug the above code to check where it get stopped. It can help to narrow down the issue. – Deepak-MSFT Feb 21 '19 at 02:41
  • Unfortunately there is no error/warning appearing on the console – Mal_a Feb 21 '19 at 04:49

0 Answers0