如何解决ShinyAlert 作为 downloadHandler 中的输入
有没有办法使用 shinyalert()
作为 downloadHandler()
的文件名输入?我想创建一个应用程序,用户可以在其中下载绘图,并且会出现“另存为:”弹出式输入消息。然后文件将被保存为任何输入。
我尝试使用 shinyalert 执行此操作,但即使我收到了弹出式输入消息,文件还是在我按下下载按钮的瞬间下载了。这是我到目前为止所拥有的:
界面:
ui <- fluidPage(
useshinyalert(),plotOutput("vmgraph"),downloadButton("downloadplot","Download Plot")
)
服务器:
server <- function(input,output) {
output$vmgraph <- renderPlot({vmgraph()})
rv <- reactiveValues(download_flag = 0)
output$downloadplot <- downloadHandler(
filename = function(){
rv$download_flag <- rv$download_flag + 1
if (rv$download_flag > 0) {
shinyalert("Save as:",type = "input",size = "m",cloSEOnesc = TRUE,cloSEOnClickOutside = TRUE,showConfirmButton = TRUE,showCancelButton = TRUE,confirmButtonText = "Save",confirmButtonCol = "#0075B8",animation = TRUE)
}
paste(input$shinyalert,".png",sep = "")},content = function(file) {
ggsave(file,plot = vmgraph(),width = 12,height = 7.7)
}
)
我使用 download_flag
作为 shinyalert()
的触发器。一直试图找到一种方法让它工作几个小时,所以任何帮助将不胜感激。
解决方法
我得到了接近你想要的东西。
关键是使用 html=TRUE
并在 Shinyalert 中放置一个 tagList
,以使“确认”按钮成为 downloadButton()
,然后使用一些 javascript:
library(shiny)
library(shinyalert)
library(ggplot2)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),useShinyalert(),plotOutput("vmgraph"),actionButton("downloadPlot","Download Plot")
)
server <- function(input,output) {
output$vmgraph <- renderPlot({plot(mtcars)})
observeEvent(input$downloadPlot,{
shinyalert("Save as:",type = "info",size = "m",html = TRUE,text = tagList(
textInput(inputId = "name",label = NULL ),downloadButton("confName","Confirm")
),closeOnEsc = TRUE,closeOnClickOutside = TRUE,showConfirmButton = FALSE,showCancelButton = TRUE,animation = TRUE
)
runjs("
var confName = document.getElementById('confName')
confName.onclick = function() {swal.close();}
")
})
output$confName <- downloadHandler(
filename = function(){
paste(input$name,".png",sep = "")},content = function(file) {
ggsave(file,plot = plot(mtcars),width = 12,height = 7.7)
}
)
}
shinyApp(ui,server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。