ShinyAlert 作为 downloadHandler 中的输入

ShinyAlert as input inside downloadHandler

有没有办法使用 Shinyalert() 作为 downloadHandler() 的文件名输入?我想创建一个应用程序,用户可以在其中下载情节,并且会出现“另存为:”弹出输入消息。然后文件将被保存为任何输入。

我尝试使用 ShinyAlert 执行此操作,但即使我收到弹出式输入消息,文件还是在我按下下载按钮后立即下载。这是我目前所拥有的:

UI:

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)