0

我需要一些闪亮的智慧,拜托。下面的代码shiny server部分运行良好;当用户单击时,它会发送一封电子邮件actionButton("mailButton", "e-mail!")ui其中包含通过 .pdf 附加的 PDF 报告gmailr。我遇到的唯一问题是没有向用户反馈是否已发送电子邮件。理想情况下,我希望在屏幕中间有一个消息框(可能带有叠加层)。当发送电子邮件时,它会告诉按下 的用户input$mailButton(并且该框可能会在 15 秒的超时后消失)。此事件大致应对应print("message sent")于以下代码中的点(这确实会打印到终端)。向用户显示电子邮件确实已发送并在发送过程中阻止输入(通常需要 4 秒)的好方法是什么?

    # email sender ------------------------------------------------------------
     observeEvent(input$mailButton,{
        isolate( {
            
            library(gmailr)
            
            params <- list(startDate = min(dashData()$mdate), 
                           endDate = max(dashData()$mdate), 
                           dfSurvey = dashData(), 
                           onePerson = input$checkOnePt, 
                           onePersonNumber = input$patientNumber, 
                           showAvg = input$checkAvgLine, 
                           alphaAvg = alphaAvg)            
            
            toAddress <- input$emailAddr
            
            if (input$checkOnePt) {
                mailSubject <- paste("Feedback graph for ", input$patientNumber)
                mailText <- paste("Hello, please see the attached report from us for ",input$patientNumber)
            } else {
                mailSubject <- paste("Feedback graph")
                mailText <- paste("Hello, please see the attached report from us.")
            }
            
                
            library(rmarkdown)
            out <- render('hwu-weekly.Rmd',
                              params = params,
                              pdf_document(),
                              envir = new.env(parent = globalenv())
                )

            
            gm_auth_configure(path  = "credentials.json")
            gm_auth(email = TRUE, cache = ".secret")
            
            email <- gm_mime() %>%
                gm_to(toAddress) %>%
                gm_from("user.us@gmail.com") %>%
                gm_subject(mailSubject) %>%
                gm_text_body( mailText ) %>% 
                gm_attach_file(filename = "hwu-weekly.pdf")
            
            gm_send_message(email)

            print("message sent")
        })
    
    })

4

1 回答 1

1

这似乎是withProgress()and的一个很好的用例showNotification()

library(shiny)

ui <- fluidPage(
  actionButton('go', 'go')
)

server <- function(input, output, session) {
  observeEvent(input$go, {
    withProgress(message = "Please Wait", {
      Sys.sleep(1)
      setProgress(0.25, detail = "1 Sec")
      Sys.sleep(1)
      setProgress(0.5, detail = "2 Sec")
      Sys.sleep(1)
      setProgress(0.75, detail = "3 Sec")
      Sys.sleep(1)
      setProgress(1, detail = "4 Sec")
   })
    showNotification("Your message here", type = "message", duration = 15)
  })
}

shinyApp(ui, server)

链接到用于通知进度指示器的闪亮文档。

您还可以将模态用于更明显的用户在继续之前必须关闭的内容:

library(shiny)

ui <- fluidPage(
  actionButton('go', 'go')
)

server <- function(input, output, session) {
  observeEvent(input$go, {
    withProgress(message = "Please Wait", {
      Sys.sleep(1)
      setProgress(0.25, detail = "1 Sec")
      Sys.sleep(1)
      setProgress(0.5, detail = "2 Sec")
      Sys.sleep(1)
      setProgress(0.75, detail = "3 Sec")
      Sys.sleep(1)
      setProgress(1, detail = "4 Sec")
   })
    showModal(modalDialog(title = 'Message', h2("Content Here")))
  })
}

shinyApp(ui, server)
于 2020-11-22T05:51:07.220 回答