【问题标题】:Shiny destroy observer once condition is met一旦满足条件,闪亮的摧毁观察者
【发布时间】:2022-01-10 17:12:00
【问题描述】:

我有一个观察器,它会查找我闪亮的应用程序正在生成的文件是否存在。制作此文件后,我想通过电子邮件向用户发送电子邮件。我现在正在做的是

o <- observe({
           
            if (!file.exists(paste0(STORAGE_PATH, input$sample_id, "/out.html"))){
                invalidateLater(1000)
            } else {
                o$destroy()
                from <<- sprintf("<Analysis_Runner@%s>", Sys.info()[4])
                to <<- input$email_address
                subject <<- paste(input$sample_id, "Anslysis Status")
                message <<- "Your analysis is finished. Please return to the 
                Shiny app to see your results"
                body <<- list(paste0("Dear ", to, "\n", message))
                sendmail(from, to, subject, body)
            }
        })

我认为destroy 调用会摆脱o,但我得到的是在out.html 生成后每秒发送一封电子邮件。如果有人可以就如何只让这封电子邮件发送 1 次提供一些建议,我们将不胜感激。我还尝试了reactivepoll 的某些版本,但似乎也不起作用。

【问题讨论】:

  • 你见过this answer吗?也许观察者无法摧毁自己。但是你可以让另一个观察者摧毁它。
  • 谢谢@michael-dewar。这实际上是让我非常接近完成这项工作的帖子。看来我的destroy() 电话应该是在电子邮件发送之后。

标签: r shiny


【解决方案1】:

您的方法效果很好。这是一个工作示例:

library(shiny)

ui <- fluidPage(
  actionButton("createfile", "Create file")
)

server <- function(input, output, session) {
  
  tempFile <- reactiveVal("")
  
  observeEvent(input$createfile, {
    tmp <- tempfile()
    file.create(tmp)
    tempFile(tmp)
  })
  
  myObserver <- observe({
    if (!file.exists(tempFile())){
    print("File not existing")
    invalidateLater(1000)
    } else {
      print("File existing - destroying observer")
      # from <<- sprintf("<Analysis_Runner@%s>", Sys.info()[4])
      # to <<- input$email_address
      # subject <<- paste(input$sample_id, "Anslysis Status")
      # message <<- "Your analysis is finished. Please return to the
      #             Shiny app to see your results"
      # body <<- list(paste0("Dear ", to, "\n", message))
      # sendmail(from, to, subject, body)
      myObserver$destroy()
    }
  })
  
}

shinyApp(ui, server)

您的file.exists 条件可能有问题?

【讨论】:

  • 谢谢@ismirsehregal。看来我的代码的主要问题是destroy() 在电子邮件之前。现在一切似乎都按预期工作了。
猜你喜欢
  • 1970-01-01
  • 2018-04-09
  • 1970-01-01
  • 2016-01-29
  • 2014-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
相关资源
最近更新 更多