1

I have this function which sends mail via the library, RDCOMClient. I have used it previously with two inputs both stemming from a dataframe using the map2 function from purrr.

Now I have extended the function to include three inputs, and I can't seem to apply it properly to a dataframe.

My data is as such:

df <- data.frame(Name = c("Name 1", "Name 2"),
                 Old_mail = c("Old mail 1", "Old mail 2"),
                 New_mail = c("New mail 1", "New mail 2"))

And my function:

library(RDCOMClient)
mail_fun <- function(name, old_mail, new_mail){

  # Open Outlook
  OutApp <- COMCreate("Outlook.Application")

   # Create mail
  outMail = OutApp$CreateItem(0)

  # Set receiver
  outMail[["To"]] = as.character(new_mail)

  # Set subject
  outMail[["subject"]] = "Some subject"

  # Set body
  outMail[["body"]] = paste(

"Dear ", name,

" \n \nBla bla bla", old_mail, "bla bla bla", sep="")

  # Send mail
  outMail$Send()

  # Pause for a second
  Sys.sleep(1)

}

Now I want to apply the function to each row such that Name 1, Old mail 1, and New mail 1 is used for name, old_mail, and new_mail, respectively for the first iteration and so forth.

As far as I know, I have to use the pmap function. I just don't know how.

Thanks in advance!

Sincerely,

Emil Blicher

1 Answers1

0

Function argument names should match the dataframe column names

COM <- function(Name, Old_mail, New_mail) {
           #browser()
           paste(Name, Old_mail, New_mail)
}

purrr::pmap(df, COM)
[[1]]
[1] "Name 1 Old mail 1 New mail 1"

[[2]]
[1] "Name 2 Old mail 2 New mail 2"
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • 1
    I realize now that the column names in the dataframe have to be the same as the names in the function. I'm kind of embarrased that it was so trivial. :D – Emil Nyboe Blicher Oct 22 '19 at 09:34