Hello. I’m using the gmailR package and a 'for loop' to send emails based off an imported dataset but I need some assistance.
I created a simplified data set example (see image below). This data set has two variables. One variable is Record (unique numeric variable: 1, 2, 3, etc) and the other variable is Section (not unique, categorical: A, B, C, D, etc)
I want to send an email where it states “Record # has been moved to Section X”. However, it should group by the Section value. Each Section should receive only 1 email with 1 message for each record. For example, Section A should receive 1 email with the 3 separate message lines listed for each record (since there are 3 Records for Section A), OR receive 1 email with 3 separate messages but within the same email chain (if that makes sense?)
Here’s my current code.
i<1
for (i in 1:nrow(RDataExample)){
subject<-paste("Moves ", date=Sys.Date(),sep="")
body<-paste("Record ",RDataExample$Record[i]," has been moved to ",RDataExample$Section[i]," on ",date=Sys.Date(),".",sep="")
msg = gm_mime() %>%
gm_from("my email") %>%
gm_to("some email") %>%
gm_subject(subject) %>%
gm_html_body(body)
gm_send_message(msg)
i<i+1
}
The issue is that this code sends a separate email for every single record in the data. Section A receives 3 different emails, but I would like it to receive only 1 email with just 3 lines listed for each record. Do you have any suggestions on how to fix this, so that each Section only gets 1 email pertaining to their Record #s? Thank you for any assistance.