Jan 18, 2016

Scheduling R Markdown Reports via Email

GA markdown report using R
R Markdown is an amazing tool that allows you to blend bits of R code with ordinary text and produce well-formatted data analysis reports very quickly. You can export the final report in many formats like HTML, pdf or MS Words which makes it easy to share with others. And of course, you can modify or update it with fresh data very easily.

I have recently been using it R Markdown for pulling data from various data source such Google Analytics API and MySQL database, perform several operations on it (merging for example) and present the outputs with tables, visualizations and insights (text).

But what about automating the whole report generation and emailing the final report as an attached document every month at a specific time?
In this post I am going to explain how to do it in Windows. If you do a search on google, you will find several threads on stackoverflow and a few good specific posts on it. However it took me sometimes to get it working and had to try different options before. That's why I am writing this quick tutorial, including screenshots, hoping you might get it your report automated faster!

1. Create your Rmarkdown report

In RStudio create a new Rmarkdown document where you will enter your R code and texts. Mine is called "Schedule_Report.Rmd" and here is what is does:

  • retrieve some data from Google Analytics API using the RGoogleAnalytics library
  • turn dates into a more friendly format
  • create a trend line chart of sessions using the ggplot2 package 

A very basic report. Remember that in Rmarkdown you can decide whether to show each chunk of code or not. I showed just the final outputs that are the table and the bar chart.

2. Create an R script that executes and email your Rmarkdown report 

Create a new R script which will:

  • locate your Rmarkdown document (set the working directory to where your report is located)
  • generate an HTML file (or pdf, MS Word) from your Rmarkdown document
  • send the HTML file via email

To email the report I have used the gmailR library which allows you to generate and send emails directly from R. To make sure the gmailR library will work, first you might need to enable the "Less Secure apps" option in your google account. Open your Google personal account and go to Sign-in and Security section, scrolll down to the bottom of the page and switch on the "Allow less secure apps".

I also made a few tries with the mailR package but without success. I guess this was because of security issues with my google account, I have gmail. Anyway the gmailR package worked perfectly so I sticked to it! Here is the code contained into my R script, which is named "Script.R".

R script scheduling reports

3. Schedule a task in Windows

From the main Windows menu, go to Programs>Accessories>System Tools>Task Scheduler (at least this is the path in my Windows edition). The task scheduler will open up:

Click on Action>Create basic task. Type a name for your task and add a short description if you like. Now select the trigger which means how often you want the task to be executed (to try it first I recommend choosing "One time"). Select the date and time and on the action field choose "Start a program".

In the "Start a Program" step, complete the fields as follows:

>Program/Script: the directory path to where to find both the executable file for R

>Add arguments: CMD BATCH followed by the path of the R script you created at step two. Remember to put the directories path between quotation marks "" like in the image below.

Schedule R reports via email

Click on next and you should now reach the last step and see a confirmation window. Press finish and voila' your task is created and it should execute correctly at the time you set.

4. Check your mail

At the time you set the task you should see the "taskeng" window popping up and disappearing after a few seconds (depending the workload you placed on your R files). Now open the mail account where you sent the report to. Did you get the email with your report attached?

In case you did not receive the email, I recommend you to:

  • Check if the the task was executed in Windows. Open the task scheduler and you will see the         list of tasks. Look for your task name. Make sure the status says "Success" and not failed;
  • If the status says failed, double check you set the task correctly as per step 3. An alternative       is  creating a .bat file separately and enter the path of the .bat file in the task scheduler;
  • As a general troubleshooting method, I also suggest opening your R console (double click     on R.exe) and execute line by line the code of your R script at step 2. This way you can             realize if there is an error inside your R code. I mean, Windows executes the task correcly         but no data is generated/sent by R.

Here below are a few issues that might prevent R from executing the code contained in your R script properly:

*To be able to send mails via gmailR package, make sure you enable the "less secured apps" option in your Google account.

**To be able to create an html document from an Rmarkdown file, make sure you have installed the latest version of pandoc library. To do that, you should, in order:
     > install.packages("installr")
     > install.pandoc()
     > Restart your machine

To recap, the process you have just automated will work as follows:
  1. Windows will start a task at the day/time you specified in the task scheduler
  2. the taskeng will open and executes your R script you create at point step 2 through R
  3. the Rmarkdown report will be converted into an HTML file and sent by email
If you like to reproduce the whole process using my files, you can find them both the Rmarkdown report and the R script at this github repository. I hope the post was helpful and will push you to use R for generating business reports.


  1. Thanks for sharing these niche piece of info's to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…
    Ethical Hacking Course in Chennai
    Hacking Course in Chennai
    Angularjs Training in Chennai
    CCNA Training in Chennai
    Salesforce Training in Chennai
    Ethical Hacking Course in Chennai
    Hacking Course in Chennai

  2. Great blog, learned many things about email database from this blog, very information. The Email Database USA is the best place to get database in USA.
    Email Database USA
    Database Provider
    Email Marketing Database
    Email Marketing Lists
    Student Database
    B2B Database
    USA Business Database
    Small Business Database
    Worldwide Email Database

  3. I'm extremely impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Anyway keep up the nice quality writing, it’s rare to see a nice blog like this one nowadays.. email marketing

  4. Just saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay informed of any updates. big data analytics

  5. Have you ever thought about writing an e-book or guest authoring on other sites? I have a blog centered on the same topics you discuss and would really like to have you share some stories/information. I know my subscribers would value your work. If you are even remotely interested, feel free to shoot me an e mail. web design business

  6. Thanks for the post and great tips..even I also think that hard work is the most important aspect of getting success Email Database

  7. I'm really impressed with your writing skills as well as with the layout on your weblog. Is this a paid theme or did you customize it yourself? Either way keep up the excellent quality writing, it is rare to see a great blog like this one these days..singapore chocolate brands

  8. Whoa! This blog looks just like my old one! It's on a completely different topic but it has pretty much the same page layout and design. Excellent choice of colors!best teeth whitening singapore

  9. I like the valuable information you provide in your articles. I’ll bookmark your blog and check again here regularly. I'm quite certain I’ll learn plenty of new stuff right here! Good luck for the next! Very nice post. I just stumbled upon your blog and wanted to say that I've really enjoyed browsing your blog posts. After all I will be subscribing to your feed and I hope you write again soon! professional cleaners

  10. Thanks for sharing these niche piece of info's to our knowledge. Here, I had a solution for my inconclusive problems & it’s really helps me a lot keep updates…

    IOT Training in Chennai

    PHP Training in Chennai

    Wordpress Training in Chennai

  11. I wish to express some appreciation to the writer just for bailing me out of this type of instance. After looking throughout the search engines and coming across recommendations that were not pleasant, I figured my life was well over. Living without the presence of solutions to the problems you have fixed as a result of your main write-up is a critical case, as well as those which may have adversely affected my entire career if I had not encountered your blog. Your good mastery and kindness in playing with all the stuff was invaluable. I don't know what I would've done if I had not discovered such a stuff like this. I can now look forward to my future. Thanks so much for this professional and results-oriented guide. I won't be reluctant to endorse your site to anyone who should receive assistance about this subject.
    website developers

  12. Pretty good post. I have just stumbled upon your blog and enjoyed reading your blog posts very much. I am looking for new posts to get more precious info. Big thanks for the useful info. sign in to Hotmail

  13. i never know the use of adobe shadow until i saw this post. thank you for this! this is very helpful. buy email list

  14. I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me. ActiveCampaign Pricing

  15. Superbly written article, if only all bloggers offered the same content as you, the internet would be a far better place..
    email search

  16. I really like your writing style, great information, thankyou for posting. find personal email

  17. شركة تنظيف بالمزاحمية
    تتمتع شركة المزاحمية للتنظيف بالعديد من المزايا التي يبحث عنها الجميع:شركة النجوم لخدمات التنظيف
    شركة تنظيف فلل بالطائف
    شركة تنظيف مجالس بالطائف
    شركة نقل عفش بالطائف

  18. السلام عليكم ورحمه الله وبركاته عملائنا الاعزاء كلنا نعرف اهميه المسابح فى حياتنا خاصه فى فصل الصيف بسبب ارتفاع درجات الحراره
    شركة الصفرات لتنظيف المسابح بالرياض
    ومن ثم يجب علينا الاعتناء بنظافه المسبح والحفاظ على حيويه المياه بداخله من اجل الحفاظ على صحتنا وصحه اطفالنا علينا ايضا ان نعرف ونتسأل ما سبب تلوث مياه المسبح
    نجد ان السابحون هم المصدر الرئيسي لتلوث مياه المسبح عادة. فهناك البعض من العادات السيئة يقوم بها زوار المسبح، الذين لا يلتزمون بقواعد النظافة العامة،
    فيتبولون في المياه أثناء السباحة دون إكتراث للأخرين. أيضاهنالك من يتجاهل تنظيف نفسه قبل السباحة، ما يؤدي إلى انتقال العرق إلى المياه.
    السيدات أيضا اللواتي لا يزلن مواد التجميل عن البشرة بشكل كامل قبل ممارسة السباحة، ينقل من خلالهن زيوت ومواد التجميل إلى المياه في المسبح
    نعتقد اننا عندما ننزل المسبح ان المياه تبدو نظيفه جدا ولكن العين المجرده لا تستطيع النظر والشعور بالبكتريا والفطريات لان حجمها يكون ظئيل جدا
    ومن ثم تقدم شركتنا افضل الطرق فى تنظيف المسابح وافضل ادوات التنظيف اتصل بشركتنا نصلكم فى الحال بأحسن الاسعار وافضل العماله المدربه فى تنظيف المسابح
    كما ان فى الأونه الاخيره نشرت الكثر من الصحف والمجلات العالميه عن الامراض التى تنجم عن عدم نظافه المسبح
    مثل احمرار العيون ومنها الأمراض الجلدية وأمراض الجهاز التنفسي وأن أكثر الأمراض شيوعاً خلال الصيف هي الدمامل، وحروق الشمس
    والالتهابات الفطرية مثل النخالية المبرقشة «عدوى فطرية في الفخذ»، وتسبب الحرارة الشديدة بتعرق الجلد، وتظهر على الجلد كحبوب مؤلمة، وحكة شديدة في جميع أنحاء الجسم،
    بقع بيضاء اللون أو داكنة على الصدر والظهر أو تقرحات وحكة بين الأصابع، التي تسمى «سعفة القدم»، التي تمثل عدوى فطرية وتنتشر في فصل الصيف وتسبب حكة في القدمين،
    وتحديداً بين الأصابع، وبقعاً متقشرة بيضاء اللون، مؤكداً أن التعرق الشديد الذي يحدث في الصيف، وخاصة بين الرياضيين، هو الذي يزيد من حدوث هذه العدوى الفطرية،
    شركة الصفرات لتنظيف المسابح بالرياض

  19. thank you for this such a good information about hotmail email login on www.hotmail.com

  20. As talked about, there is intense challenge between the brands in the market. When we search any administration on the web crawler, we get 100s of results.ExcelR Digital Marketing Course in Sydney

  21. I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. iniciar una sesion en hotmail

  22. thanks alot for sharing amazing article again keep it up
    hotmail sign up