If I understand your question well, something like that might do the job
library(sendmailR)
## Set a counter increment
counter <- 0
## Initialising the text file
file_name <- "file_out.txt"
## Mail details
from <- "you@account.com"
to <- "recipient@account.com"
subject <- "Email Subject"
body <- "Email body."
mailControl=list(smtpServer="serverinfo")
attachmentPath <- file_name
attachmentName <- file_name
## Loop example
while(class(counter) == "numeric") {
## Pausing the loop for the example
Sys.sleep(1)
## Updating the time
new_time <- Sys.time()
## Printing the time out of R
write(as.character(new_time), file = file_name, append = TRUE)
## Check the last time
old_time <- scan(file_name, sep="\n", what="char(0)", skip = counter)
## Incrementing the counter
counter <- counter + 1
## Compare both times
time_difference <- as.POSIXlt(new_time)-as.POSIXlt(old_time)
## time difference is too long
if(time_difference > 3) {
counter <- FALSE
}
## Exiting the loop for the example
if(counter > 5) {
counter <- FALSE
}
}
## Send the email if the loop was exited on a FALSE counter
if(class(counter) == "logical" && counter == FALSE) {
attachmentObject <- mime_part(x = attachmentPath, name = attachmentName)
bodyWithAttachment <- list(body, attachmentObject)
sendmail(from = from, to = to, subject = subject, msg = bodyWithAttachment, control = mailControl)
}
More details on printing the Sys.time()
in a file, you can follow the procedure here: Write lines of text to a file in R
More details on sending emails at the end of the loop: How to send an email with attachment from R in windows.