8

I am currently doing several simulations in R that each take quite a long time to execute and the time it takes for each to finish varies from case to case. To use the time in between more efficiently, I wondered if it would be possible to set up something (like a e-mail notification system or similar) that would notify me as soon a a chunk of simulation is completed.

Does somebody here have any experience with setting up something similar or does someone know a resource that could teach me to implement a notification system via R?

Fred
  • 410
  • 3
  • 12
  • 2
    Set up an alarm? Assuming you're not going to be working remotely? It's interesting to note that R's `alarm` function is a one-liner. – NelsonGon Jun 22 '19 at 16:39
  • 1
    @NelsonGon an alarm would definitely be a good solution, although I am aiming to implement a remote mechanism – Fred Jun 22 '19 at 16:43
  • 1
    What would that one-liner be if you mentioned it? – Claudiu Papasteri Jun 22 '19 at 16:44
  • 1
    @ClaudiuPapasteri it `cat`s `\a` and flushes the console. – NelsonGon Jun 22 '19 at 16:45
  • 2
    @Dan Wrap [this](https://stackoverflow.com/questions/23412265/how-do-you-send-email-from-r) in your chunks? Not sure how you could "automate" it for all chunks. – NelsonGon Jun 22 '19 at 16:46
  • 2
    The `mailR`-package could be the solution you are searching for, at least it was for me when I let my program scrape over night. – Max Teflon Jun 22 '19 at 16:47
  • 2
    Thank you both for the input, I'll check out `mailR` and `sendmailR` to see if and how I could automate it across the chunks – Fred Jun 22 '19 at 16:52
  • 2
    https://stackoverflow.com/questions/23412265/how-do-you-send-email-from-r; https://stackoverflow.com/questions/2885660/how-to-send-an-email-with-attachment-from-r-in-windows – the-mad-statter Jun 22 '19 at 16:56

2 Answers2

7

I recently saw an R package for this kind of thing: pushoverr. However didn't use it myself - so not tested how it works. But seems like it might be useful in your case.

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
  • 1
    I just tested it, that package is amazing. Feels quite awesome to send push notifications to your phone through R, thanks for sharing! – Fred Jun 22 '19 at 22:48
2

I assume you run the time consuming simulations on a server, correct? If these run own you own PC, your PC will be slow as hell anyway and I would not see something beneficial in sending a mail to myself.

For long calculations: Run them on a virtual machine, I use the following workflow for my own calculations.

  1. Write your R script. Important: Write a .txt file when the calculation file in the end. The shell script will search in a loop for the file to exist.
  2. Copy that code an save it as Python script. I tried one day to get MailR running a Linux and it did not work. This code worked on the first try.
#!/usr/bin/env python3
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
email_user = 'youownmail@gmail.com'
email_password = 'password'
email_send = 'theothersmail.com'
subject = 'yourreport'
msg = MIMEMultipart()
msg['From'] = email_user
msg['To'] = email_send
msg['Subject'] = subject
body = 'Calculation is done'
msg.attach(MIMEText(body,'plain'))
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
msg.attach(part)
text = msg.as_string()
server = smtplib.SMTP('smtp.gmail.com',587)
server.starttls()
server.login(email_user,email_password)
server.sendmail(email_user,email_send,text)
server.quit()

Make sure you are allowed to run the script.

  1. sudo chmod 777 /path/script.R sudo chmod 777 /path/script.py

  2. Run both your script.R and script.py inside a script.sh file. It looks the the following:

R < /path/script.R --no-save  
while [ ! -f /tmp/finished.txt ]
do
  sleep 2
done
python path/script.py

This may sound a bit overwhelming if you are not familiar with these technologies, but think this is a pretty much automated workflow, which relieves your own resources and can be used "in production". (I use this workflow to send me my own stock reports).

DSGym
  • 2,807
  • 1
  • 6
  • 18