简体   繁体   中英

Shiny trigger server function from UI

I would like to create an action button in the UI that, when clicked, activates the following function that send emails in the server:

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()
 }
 }

Currently in the UI I have the following but does not work:

ui<-fluidpage(

actionButton("sendmail","Send confirmation")

)

Any suggestions?

*the function works as I tested it without actionButton

You can use observeEvent to trigger certain functionality whenever the sendmail button is clicked, as follows:

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()
 }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM