来自 UI 的闪亮触发服务器功能

Shiny trigger server function from UI

我想在 UI 中创建一个操作按钮,单击该按钮会激活以下在服务器中发送电子邮件的功能:

server <- function(input, output){

output$sendmail <- function(){
  
 #install.packages("RDCOMClient")
 # library("RDCOMClient")
 ## init com api
 OutApp <- COMCreate("Outlook.Application")
 ## create an email 
 outMail = OutApp$CreateItem(0)
 ## configure  email parameter 
 outMail[["To"]] = "youremail@outlook.com"
 outMail[["subject"]] = "Greetings"
 outMail[["body"]] = "Hello"
 ## send it    
 path_to_file = "C:/Users/Desktop/myFile.pdf"
 outMail[["Attachments"]]$Add(normalizePath(path_to_file))
 outMail$Send()
 }
 }

目前在 UI 我有以下但不起作用:

ui<-fluidpage(

actionButton("sendmail","Send confirmation")

)

有什么建议吗?

*我在没有 actionButton

的情况下测试了该功能

您可以使用 observeEvent 在单击 sendmail 按钮时触发某些功能,如下所示:

observeEvent(input$sendmail, {
 ## init com api
 OutApp <- COMCreate("Outlook.Application")
 ## create an email 
 outMail = OutApp$CreateItem(0)
 ## configure  email parameter 
 outMail[["To"]] = "youremail@outlook.com"
 outMail[["subject"]] = "Greetings"
 outMail[["body"]] = "Hello"
 ## send it    
 path_to_file = "C:/Users/Desktop/myFile.pdf"
 outMail[["Attachments"]]$Add(normalizePath(path_to_file))
 outMail$Send()
 }
)