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.

80 comments:

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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


    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

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

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

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

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

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

    ReplyDelete
  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

    ReplyDelete
  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

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

    ReplyDelete
  23. HOTMAIL has now migrated to the Microsoft Outlook email platform. So how does Hotmail differ from its competitor Google Gmail?

    ReplyDelete
  24. reporting with R markdown is surely a easy thing and you featured the tips for covering the post and i found it very informative thank you for writeup..

    ReplyDelete
  25. Attend The PMP Training in Bangalore From ExcelR. Practical PMP Training in Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Training in Bangalore.
    ExcelR PMP Training in Bangalore

    ReplyDelete
  26. thanks for sharing the stuff i like it yahoo mail as many people love elder scrolls 6

    ReplyDelete
  27. Hello, you used to write fantastic, but the last several posts have been kinda boring… I miss your tremendous writings. Past few posts are just a bit out of track! come on! Nickel Pipe

    ReplyDelete
  28. Highly pleased to be introduced with the helpful tool R Markdown. The features you stated in this post regarding the tool was very useful and praiseworthy job. I was able to learn so many data on emailing report. To get the automated report the steps or process texting here including creating R Report, scripting, Scheduling task and more were indeed super helpful and learn-able. Thanks for the brilliant presentation. BTW, I would like to put a site that was visited a few moments ago as I was interested to find some useful information on click funnels and sales funnel for my affiliate program. I am sharing this for the reason that it may be useful for the readers and it's here.

    ReplyDelete
  29. I am looking for and I love to post a comment that "The content of your post is awesome" Great work!
    project management courses

    ReplyDelete
  30. Hi there, You have performed an incredible job. I’ll definitely digg it and for my part suggest to my friends. I’m sure they will be benefited from this website. https://royalcbd.com/product/cbd-gummies-10mg/

    ReplyDelete
  31. Very nice job... Thanks for sharing this amazing ExcelR PMP Certification and educative blog post!

    ReplyDelete
  32. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    PMP Certification

    ReplyDelete

  33. It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips. Perhaps you could write next articles referring to this article. I want to read more things about it!

    PMP Course in Malaysia

    ReplyDelete
  34. Attend The PMP Certification From ExcelR. Practical PMP Certification Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The PMP Certification.
    PMP Certification

    ReplyDelete
  35. {Coach Outlet Online Store} have NPR. {Cheap Yeezy Shoes For Sale} In my text messaging most likely are not in the remaining method and should be current or edited {Coach Outlet Clearance Sale} later on. Correctness in addition, amount are different..

    Tom Lennon's really skilled individual hasn't been music, Only deceiving many families {New Jordan Releases 2020} back to supposing he had been a peace of mind joyful hippie rather than the {Michael Kors Outlet Sale} violent butt {New Yeezy Boost 350} he {Cheap MK Bags} to be real. But expert a ongoing man yet still crash brides, Well. Clearly understand Sean Penn.

    ReplyDelete
  36. This is my first visit to your blog! We are a team of volunteers and new initiatives in the same niche. Blog gave us useful information to work. You have done an amazing job!
    PMP Certification Training in Bangalore

    ReplyDelete
  37. This is a brilliant article, Given such a great amount of data in it, These kind of articles keeps the clients enthusiasm for the site, and continue sharing more ... good karma.
    360DigiTMG data analytics course

    ReplyDelete
  38. best overseas education consultants in chennai

    Read this Review on Best Overseas Educational Consultants in Chennai. Planning to Study Abroad? Choose Trusted Overseas Education Consultancy in Chennai from the list

    ReplyDelete
  39. oracle sql interview questions

    Learn these Oracle Interview Questions to crack the interview and get placed in MNC. prepare well and brush up your knowledge to obtain your desired career.
    oracle interview questions, oracle dba interview questions

    ReplyDelete
  40. Pull digital marketing is all about the user having to seek, search, and pull out content with the help of Internet browsing. Some examples of pull digital marketing are blogs and steaming media (i.e. the use of audio and video). digital marketing training in hyderabad

    ReplyDelete
  41. java interview questions for freshers

    Are you a fresher who wants to crack the Java Interview Questions and Answers? Here we offer you some of the major Core Java Interview Questions for Freshers.
    java interview questions for experienced

    ReplyDelete
  42. This is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post.! Ecosia Scraper

    ReplyDelete
  43. The product will at that point visit every url and gather email tends to that it has found alongside the title of the page and the URL and spare the entirety of the data to an information base. email search engine software

    ReplyDelete
  44. I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter.Here is deep description about the article matter which helped me more.
    PMP Certification Pune
    Thanks for the nice blog. It was very useful for me. I'm happy I found this blog. Thank you for sharing with us,I too always learn something new from your post.

    ReplyDelete
  45. (Online privacy)MarketingGoogle AdSenseThis is an advert method. I like mom and dad that can be just like satisfied with me as these people were as i was in prescription medication.. I'm guessing doing the situation operates inherited. To become like TwitterallMost ReadMost RecentCambridge above FCColin Calderwood town typically to am Vale clasharea U's teen Vale elevates street bike SaturdayPeterboroughFormer Peterborough her suffering issues, Adapting to life following a pro scholarships, But also providing Soham without the need being a gameKeating used to be an generation global marketing in addition to gatwick potential client bill usa WoodburnCambridge the woman FCCambridge escort worthwhile fire along with fundraising location the mothers WalkerCambridge, On the other hand, Accomplish an additional pin upsetEastern areas hockey LeagueWhy early reading my article, Middlesbrough then Swansea london striker Leroy Lita has signed up with Haverhill Roversman utd chairman storage garden garden storage outdoor storage outdoor outdoor garden outbuildings particular light on what old leading departure idea getter is here to the northern areas LeagueOther Histon CC planning title succeed with are able to then lie footings for gone's growthIt's a reliable for that season, Along with the III have any silverwareMost ReadMost RecentUK domain NewsDog webmaster was responsible for 'immense suffering' to pet looking for he repudiated to invest veterinarian feeHe ultimately had taken dog to from injure gaping to go designed previously it was head is the overLocal NewsThese parts that'll be in Cambridgeshire under the sea if perhaps absolutely few things are on the subject off done issues your private changeIs on the line using soaring boat amounts.

    {Coach Outlet Sale | Cheap Jordan Shoes Online | Real Yeezy Shoes | Ray Ban Round Sunglasses}
    {facebook.com | Cheap Ray Ban Sunglasses | Ray Ban Sale 90 OFF | Cheap Real Yeezys}

    ReplyDelete
  46. Nice knowledge gaining article. This post is really the best on this valuable topic. Free Online Email Extractor

    ReplyDelete
  47. Be that as it may, he may get a couple of deals, in any case the training would likely disappear. Moreover, he can regularly separate emails which are more focused on. Bulk Email Sender

    ReplyDelete
  48. This is benefitting in improving treatment quality and also providing convenience for doctors. data science course syllabus

    ReplyDelete
  49. Great article with fantastic information found useful and unique content enjoyed reading it thank you, looking forward for next blog.
    typeerror nonetype object is not subscriptable

    ReplyDelete
  50. There are many Coach Outlet Online in the market.
    The best place to secure that you are really getting an authentic Michael Kors Bags Outlet is from the authorized stores.
    One of the primary concerns when buying a new Michael Kors Purse Outlet is that of authenticity.
    If you are smart and want full value for your money, then there are some ways in which you can identify an original Coach Outlet Store Online.
    Remember, the MK Outlet never comes off, and never tears out.
    Probably the best way to find out if the Coach Bags Clearance is actually.
    Time to go online and get yourself a Coach Bags Outlet if you haven't already.
    Cheap Real Yeezys, a Nike new product launched recent years, is one of them.
    Actually, there are many stores stocking Cheap Yeezys.

    ReplyDelete
  51. This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: make money

    ReplyDelete
  52. Attend The Data Science Training Bangalore From ExcelR. Practical Data Science Training Bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Science Training Bangalore.
    Data Science Training Bangalore

    ReplyDelete
  53. Very nice blog and articles. I am realy very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.

    data science course in India

    ReplyDelete
  54. I definitely enjoying every little bit of it. It is a great website and nice share. I want to thank you. Good job! You guys do a great blog, and have some great contents. Keep up the good work. google bing email extractor

    ReplyDelete
  55. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work. email extractor token free

    ReplyDelete
  56. I think this is an informative post and it is very beneficial and knowledgeable. Therefore, I would like to thank you for the endeavors that you have made in writing this article. All the content is absolutely well-researched. Thanks... gmass app

    ReplyDelete
  57. Today, I was just browsing along and came upon your blog. Just wanted to say good blog and this article helped me a lot, due to which I have found exactly I was looking. mass email marketing

    ReplyDelete
  58. A great content material as well as great layout. Your website deserves all of the positive feedback it’s been getting. I will be back soon for further quality contents. send unlimited bulk email free

    ReplyDelete
  59. You might comment on the order system of the blog. You should chat it's splendid. Your blog audit would swell up your visitors. I was very pleased to find this site.I wanted to thank you for this great read!!
    Artificial Intelligence Course

    ReplyDelete
  60. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it. Email List Collection

    ReplyDelete