Home > Enterprise >  Shiny trigger server function from UI
Shiny trigger server function from UI

Time:04-26

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"]] = "[email protected]"
 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

CodePudding user response:

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"]] = "[email protected]"
 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()
 }
)
  • Related