Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic E-mailing of pdf graphical output at specific times from R

Tags:

email

r

pdf

I have some scripts I'd like to run each morning at 6am. These scripts produce some pdfs of graphical output into a file: foo.pdf

I'd like my system (let's say Win 7, >= R 2.13) to email me these pdf's once the system has finished running the scripts.

Which is the best package - and most robust way of setting it up - to have these reports emailed to me directly via attachment from R?

Are there any 'cool' extensions to this (like sink() -ing report text output into the body of the email)?

Thanks in advance for any advice.

like image 284
n.e.w Avatar asked Apr 23 '12 05:04

n.e.w


2 Answers

You can harness the power of a package that can handle emails coupled with a chron job. On Windows 7, I've achieved something akin to this using Windows Task Scheduler. Basically, you set it to run a particular script at a specified time.

like image 113
Roman Luštrik Avatar answered Sep 28 '22 09:09

Roman Luštrik


I have a script running daily, and had lots of problems to get it to run. Take a look at Roman's link for the attachment and R code first. This is about the non-R part in Windows 7.

I had problems running the R script directly from Windows Task Scheduler, so I scheduled a batch file to run every day as follows:

@echo on
"C:\Rpath\R-2.15.1\bin\i386\Rcmd.exe" BATCH "C:\filepath\filetorun.R"

That's about the simplest you can get, but Quick R was a starting point.

Depending on your computer's settings, you might have to fiddle with the task scheduler. If it's a server type that's always on, then you shouldn't have too many issues (and you know what you're doing). If you have to log off and use a password to login or access a shared drive, you'll have to do some of the following. Also, I don't know if admin rights is a necessity or not.

Open Task Scheduler, make a new task, and open its properties window.

Under General, check the user account and select "Run whether user is logged on or not" and UNcheck "Do not store password." This will allow your script to run if you're logged off (I don't think it works when Locked). When you click Ok, it will ask for your password.

Basic setup: The Trigger is "On a schedule" and Advanced is Enabled. Under Actions, select "Start a program" with the Program/script as the .bat file.

Under Conditions, uncheck "Start the task only if comp is idle" and check "Wake the computer to run this task." Under Settings, check "Allow task to be run on demand," check "If the running task does not end..," and at the bottom, select "Stop the existing instance." These options might be necessary, though I'm not as sure about these.

Another trick is if your company has you switch passwords every once in a while. Open and close the task after changing so that it asks for your password again. Enter the new one or else it can't log in and won't run your script.

like image 33
ARobertson Avatar answered Sep 28 '22 07:09

ARobertson