<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/sending-emails-from-r-exercises/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.r-exercises.com/2017/08/14/sending-emails-from-r/"> R-exercises</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://i0.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/at-2440261_960_720.png" target="_blank"><img sizes="(max-width: 300px) 100vw, 300px" src="https://i1.wp.com/www.r-exercises.com/wp-content/uploads/2017/08/at-2440261_960_720-300x300.png?resize=300%2C300" alt="" class="wp-image-7473" data-recalc-dims="1"></a>When monitoring a data source, model, or other automated process, it’s convienent to have method for easily delivering performance metrics and notifying you whenever something is amiss. One option is to use a dashboard; however, this requires active time and effort to grab numbers and catch errors. An alternative approach is to send an email alert on the performance of the process. In this exercise set, we will explore the email approach using the <code>mailR</code> package.</p>
<p>Exercises in this section will be solved using the <code>mailR</code> package as well as basic HTML and CSS. It is recommended to take a look at the <code>mailR</code> documentation before continuing.</p>
<p>Answers to the exercises are available <a rel="nofollow" href="http://r-exercises.com/2017/08/14/sending-emails-from-r-solutions/" target="_blank">here</a>.</p>
<p><strong>Exercise 1</strong><br>
Let’s begin by sending “Hello World. This is my email!” as the <code>body</code> parameter from yourself to yourself.</p>
<p><strong>Exercise 2</strong><br>
By passing in a vector for the <code>to</code> parameter, you can send the email to multiple recipients. Send the above email to yourself and a friend.</p>
<p><strong>Exercise 3</strong><br>
So far, your emails have had no subject. Send the email from Exercise 1 to yourself with “Email Testing” for the <code>subject</code> parameter.</p>
<p><strong>Exercise 4</strong><br>
With this package, we can take full advantage of CSS when constructing the body of an email. Send the email from the previous exercise from yourself to yourself where “Hello World.” is now <code>red</code> and “This is my email!” is now <code>blue</code>.</p>
<p>Note: make sure that <code>html = TRUE</code>.</p>
<div>
<aside><img src="http://www.r-exercises.com/2017/08/14/sending-emails-from-r/"></aside>
<div><strong>Learn more</strong> about html functionality and web connection in the online course <a rel="nofollow" href="http://www.r-exercises.com/product/a-complete-journey-to-web-analytics-using-r-programming-tool/" target="_blank">A complete journey to web analytics using R tool</a>. In this course you will learn how to:
<ul>
<li>Perform a web based analytic question start to end</li>
<li>Learn how to import data from different online platforms such as twitter</li>
<li>And much more</li>
</ul>
</div>
</div>
<p><strong>Exercise 5</strong><br>
If you write a complex email containing images, dynamic elements, etc. as an HTML file, then you can reference this file with the <code>body</code> parameter. Create an HTML file containing “Hello World. This is my email!” called <em>my_email.html</em>. Send this email to yourself.</p>
<p><strong>Exercise 6</strong><br>
Using <code>knitr</code>, you can compile HTML files. Compile the default <code>knitr</code> document that uses the <code>mtcars</code> dataset to an HTML file and email this to yourself.</p>
<p><strong>Exercise 7</strong><br>
Create a new R script called <em>mailr_six.R</em> containing your code from the above exercises and attach that to your email by referencing the file path to <em>mailr_six.R</em> in the <code>attach.files</code> parameter. Send this email from yourself to yourself.</p>
<p><strong>Exercise 8</strong><br>
The attached R script above does not have a description or a name. Add these in the <code>file.descriptions</code> and <code>file.names</code> parameters, respectively. Send the resulting email to yourself.</p>
<p><strong>Exercise 9</strong><br>
Just as with the recicipents, you can attach multiple files, descriptions, and names by passing in vectors to the respective parameters. Create a new R script called <em>mailr_eight.R</em> containing your code from the above exercises and attach both <em>mailr_six.R</em> and <em>mailr_eight.R</em> to your email. Send the resulting email to yourself.</p>
<p><strong>Exercise 10</strong><br>
Create a new R script where a random integer called <em>important_number</em> is generated. If <em>important_number</em> is even, then send an email to yourself notifying you that <em>important_number</em> is even.</p>
<div>
<h3>Related exercise sets:</h3>
<ol>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/01/24/building-shiny-app-exercises-part-5/" title="Building Shiny App Exercises (part 5)" target="_blank">Building Shiny App Exercises (part 5) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2016/12/12/building-shiny-app-exercises-part-1/" title="Building Shiny App exercises part 1" target="_blank">Building Shiny App exercises part 1 </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/2017/07/13/parallel-computing-exercises-foreach-and-doparallel-part-2/" title="Parallel Computing Exercises: Foreach and DoParallel (Part-2)" target="_blank">Parallel Computing Exercises: Foreach and DoParallel (Part-2) </a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/start-here-to-learn-r/" target="_blank">Explore all our (>1000) R exercises</a></li>
<li><a rel="nofollow" href="http://www.r-exercises.com/r-courses/" target="_blank">Find an R course using our R Course Finder</a> directory</li>
</ol>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.r-exercises.com/2017/08/14/sending-emails-from-r/"> R-exercises</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/XtypEasHttg" height="1"></a></div>