0

Below is an easy example of shiny app with JavaScript code to help download plots. At the moment the name of downloaded PNG file is "plot.png". I would like to use the element created from htmlOutput("text") to add to the name of PNG file. In this case then the name would be: "2019-01-31 plot.png"

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)

[EDIT] After getting an asnwer from @Stéphane Laurent I am facing an issue with IE, whereas with Firefox works without any Problems. Any ideas, tips are welcome!

Mal_a
  • 3,670
  • 1
  • 27
  • 60

1 Answers1

2

You can do

a.download = $("#text").html() + " plot.png";
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thats exactly what i Need! Thanks a lot.. I have one small additional question to it:if my htmlOutput looks like that: `HTML(paste("",eopm,""))`, how i can get only the date instead of all `span style=\"font-weight:...` – Mal_a Feb 20 '19 at 10:12
  • 1
    @Mal_a `$("#text span").html()`. This returns `" 2019-01-31 "` (note the surrounding white spaces). Use `sep=""` in `paste`to get rid of the white spaces, or, better, use `paste0` instead of `paste`. – Stéphane Laurent Feb 20 '19 at 10:17
  • Thanks a lot! It works perfectly but weirdy only in Firefox, IE does not support it? – Mal_a Feb 20 '19 at 11:03
  • @Mal_a I don't know... I use Chrome only. – Stéphane Laurent Feb 20 '19 at 12:17
  • @Mal_a https://stackoverflow.com/questions/18394871/download-attribute-on-a-tag-not-working-in-ie – Stéphane Laurent Feb 20 '19 at 12:20