我已经看到许多文章使用JavaScript在IE中下载文件,但是由于我对JavaScript的了解非常少,我将不胜感激。下面我们有一个闪亮的应用程序,该程序在Firefox中可以完美运行,但是在IE中尝试时,根本不会出现下载窗口。
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)
我如何使其在IE中工作?
[编辑]
在我的帖子下发表评论后,我设法借助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)
但是我无法从Plotly.Snapshot.toImage
函数检索对象,我认为它是url
,但是正如您在我的代码中看到的那样,它不起作用...
我将不胜感激!