微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ShinyAlert 作为 downloadHandler 中的输入

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?