<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>blastula package on R Views</title>
    <link>https://rviews.rstudio.com/tags/blastula-package/</link>
    <description>Recent content in blastula package on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Wed, 04 Sep 2019 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/blastula-package/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>How to Send Custom E-mails with R</title>
      <link>https://rviews.rstudio.com/2019/09/04/how-to-send-custom-e-mails-with-rstudio-connect/</link>
      <pubDate>Wed, 04 Sep 2019 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2019/09/04/how-to-send-custom-e-mails-with-rstudio-connect/</guid>
      <description>
        


&lt;p&gt;A common business oriented data science task is to programatically craft and send custom emails. In this post, I will show how to accomplish this with R on the &lt;a href=&#34;https://www.rstudio.com/products/connect/&#34;&gt;RStudio Connect&lt;/a&gt; platform (a paid product built for the enterprise) using the &lt;a href=&#34;https://cran.r-project.org/package=blastula&#34;&gt;&lt;code&gt;blastula&lt;/code&gt;&lt;/a&gt; package.&lt;code&gt;blastula&lt;/code&gt; provides a set of functions for composing high-quality HTML e-mails that render across various e-mail clients, such as gmail and outlook, and also includes tooling for sending out those e-mails via SMTP, the standard protocol for electronic mail transmission between different e-mail providers. At the bottom of the post you can find a link to documentation showing how to publish email with &lt;code&gt;blastula&lt;/code&gt; via an SMTP server without using RStudio Connect.&lt;/p&gt;
&lt;p&gt;As an example, we’ll pretend that I work in a marketing analytics department at an insurance company. I’m responsible for a marketing report, created with the &lt;code&gt;rmarkdown&lt;/code&gt; package, that tracks the number of bound policies from different marketing activities:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;## Warning: package &amp;#39;tibble&amp;#39; was built under R version 3.5.2&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## Warning: package &amp;#39;knitr&amp;#39; was built under R version 3.5.2&lt;/code&gt;&lt;/pre&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr class=&#34;header&#34;&gt;
&lt;th align=&#34;left&#34;&gt;Mktg_Activity&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;Policies&lt;/th&gt;
&lt;th align=&#34;right&#34;&gt;Target&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;Partnerships&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;345&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;320&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;E-mail Mktg&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;434&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;410&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;odd&#34;&gt;
&lt;td align=&#34;left&#34;&gt;Direct Mail&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;240&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;235&lt;/td&gt;
&lt;/tr&gt;
&lt;tr class=&#34;even&#34;&gt;
&lt;td align=&#34;left&#34;&gt;Radio&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;128&lt;/td&gt;
&lt;td align=&#34;right&#34;&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Having written the report with R Markdown, I will publish the script to RStudio Connect and have Connect create and send an e-mail for me. Once this is done, I’ll turn on both the &lt;a href=&#34;https://docs.rstudio.com/connect/user/settings-panel.html#content-schedule&#34; target=&#34;&amp;quot;_blank&#34;&gt;&lt;em&gt;scheduler&lt;/em&gt;&lt;/a&gt; and &lt;em&gt;Send email after update&lt;/em&gt; options to have Connect re-run the report on a set schedule. By default, the e-mail generated by RStudio Connect looks something like this:&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;screenshot.png&#34; /&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Because we haven’t done anything to customize the e-mail notification yet, Connect generated a standard out-of-the-box e-mail. It used the published document name for the e-mail subject, included a link to the report, as well as the time stamp of when it was executed. The e-mail also contains the actual report as an attachment, which can be downloaded and viewed. This is already useful, but in this case I’d like to customize the e-mail to make it more tailored to fit my team’s needs. This is where the &lt;code&gt;blastula&lt;/code&gt; package comes in.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;blastula&lt;/code&gt; allows you to create and send e-mails using R. It works similarly to the Shiny package, but instead of writing R code to create an interactive application, you write R code to create an HTML e-mail that can be rendered across a wide variety of e-mail providers. Once you’ve programmatically created an HTML e-mail, &lt;code&gt;bastula&lt;/code&gt; can also be used to send out that e-mail programmatically.&lt;/p&gt;
&lt;p&gt;To create your custom e-mail, simply add a new R code chunk at the bottom of your R Markdown script. You can use the &lt;a href=&#34;https://bookdown.org/yihui/rmarkdown/r-code.html&#34;&gt;code chunk option&lt;/a&gt; &lt;code&gt;include = FALSE&lt;/code&gt; so that your R code isn’t printed in your actual RMardown report, since that wouldn’t be very helpful to whomever is reading the report:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# load the blastula package
library(&amp;quot;blastula&amp;quot;)

# create a simple e-mail
email &amp;lt;- compose_email(body = &amp;quot;Insert your e-mail body here&amp;quot;,
                       footer = &amp;quot;Insert your e-mail footer here&amp;quot;)

# preview e-mail in Viewer pane
preview_email(email)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;email1_screenshot.png&#34; /&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;&lt;code&gt;blastula&lt;/code&gt; supports string interpolation, meaning it can display the value of an R variable rather than simply printing your R code as plain text. The way to tell &lt;code&gt;blastula&lt;/code&gt; what is R code vs. what’s plain text, is by adding curly braces around anything you want to be interpreted as R:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create an e-mail with R code

email_body &amp;lt;- 
&amp;quot;
Hi! This new report was generated at {Sys.time()}
&amp;quot;

email_footer &amp;lt;- 
&amp;quot;
Please contact *support@acme.com* with any questions
&amp;quot;

email &amp;lt;- compose_email(body = email_body,
                       footer = email_footer)

# preview e-mail in Viewer pane
preview_email(email)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;email2_screenshot.png&#34; /&gt;
 &lt;/p&gt;
&lt;p&gt;You’ll notice that not only can you include R code, but you can also supply Markdown syntax (notice how we italicized some of the footer text). In addition to all this, you can use helper functions included in the &lt;code&gt;blastula&lt;/code&gt; package to add other elements to your e-mail. For example, you can add a &lt;code&gt;ggplot2&lt;/code&gt; plot as an image using the &lt;code&gt;add_ggplot&lt;/code&gt; function:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# create an e-mail with a plot
library(ggplot2)
plot &amp;lt;- ggplot(tb, aes(Mktg_Activity, Policies)) + geom_bar(stat = &amp;quot;identity&amp;quot;)


email_body &amp;lt;- 
&amp;quot;
Hi! This new report was generated at {Sys.time()} \\


{add_ggplot(plot, width = 5, height = 3)}

&amp;quot;

email_footer &amp;lt;- 
&amp;quot;
Please contact *support@acme.com* with any questions
&amp;quot;

email &amp;lt;- compose_email(body = email_body,
                       footer = email_footer)

# preview e-mail in Viewer pane
preview_email(email)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;email3_screenshot.png&#34; /&gt;
 &lt;/p&gt;
&lt;p&gt;Now that we’ve created the e-mail programmatically, the next step is to send it out. Because my company uses RStudio Connect to host reports and send e-mail notifications, I need to add the following two lines of code to the bottom of my report so that RStudio Connect knows what to use as the e-mail body:&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;# Use Blastula&amp;#39;s message as the email body in RStudio Connect.
rmarkdown::output_metadata$set(rsc_email_body_html = email$html_str)
rmarkdown::output_metadata$set(rsc_email_images = email$images)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before this point, the blastula code we’d written generated a nice HTML e-mail, which in this case had been saved to a variable I called &lt;code&gt;email&lt;/code&gt;. However Connect didn’t know that. To remedy this, we saved the e-mail we created to the &lt;code&gt;output_metadata&lt;/code&gt; object. The &lt;code&gt;output_metadata&lt;/code&gt; object contains “metadata”, or information, some of which Connect uses. Two of those items are &lt;code&gt;rsc_email_body_html&lt;/code&gt; and &lt;code&gt;rsc_email_images&lt;/code&gt;, which Connect uses to build the HTML notification e-mail it sends out. For consistency, you can always assign both of these items at the end of your R Markdown report, even if initially it does not contain embedded images.&lt;/p&gt;
&lt;p&gt;If you do not wish to use RStudio Connect to send messages, you can also use the &lt;code&gt;smtp_send()&lt;/code&gt; function to send your e-mail via a SMTP server. For instructions, check out the package’s “Sending Email Using SMTP” vignette on &lt;a href=&#34;https://github.com/rich-iannone/blastula/blob/master/vignettes/sending_using_smtp.Rmd&#34;&gt;github&lt;/a&gt;. To learn more about crafting custom e-mails, check out the &lt;a href=&#34;https://cran.r-project.org/web/packages/blastula/blastula.pdf&#34; target=&#34;_blank&#34;&gt;blastula documentation&lt;/a&gt; and the &lt;a href=&#34;https://docs.rstudio.com/connect/user/r-markdown.html#r-markdown-email-customization&#34; target=&#34;_blank&#34;&gt;RStudio Connect User Guide&lt;/a&gt;.&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2019/09/04/how-to-send-custom-e-mails-with-rstudio-connect/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
