javascript - 为什么我的 Shiny plot 下载应用无法在 IE 中运行?
问题描述
我已经看到很多帖子在 IE 中使用 JavaScript 下载文件,但是由于我对 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
但正如您在我的代码中看到的那样,它不起作用......我将不胜感激任何帮助或提示!
解决方案
推荐阅读
- assembly - 如何定义可在 C 中使用的 ARM 汇编函数
- css - 在 Ionic 4 中更改 css
- linux - 在 bash_profile 中自定义 git 命令以自动索引到下一个分支
- java - 主题:使用回溯(而不仅仅是递归 DFS)背后的直觉
- forms - 使用 URL/查询字符串预填充输入表单值需要什么工作?
- apache-kafka - IoT - 多个 Kafka 生产者将消息发布到同一主题
- javascript - 当我使用 jquery 试图从另一个页面检索一些文本时,为什么我得到 [object Object]
- java - 使用 mongoClient 在 mongoDb 中插入日期
- android - 使用带有名称和密码的指纹身份验证登录
- openseadragon - 如何创建可缩放的文本