install.packages("blastula")
library(blastula)
Send Reports and Emails with Ease Using R and GitHub
Introduction
Automating daily email reports can save time and effort. With the Blastula package in R and GitHub, you can schedule and send emails seamlessly—no need for task schedulers (that require your laptop to stay on), paid cloud services, or Office365 accounts. Just code your analysis, set the timer, and let the process run automatically.
Let us begin with installing and loading the required package
Step 1: Generating a password file
Creating a password file in Blastula secures your credentials by keeping them hidden from the code, reducing the risk of accidental exposure.
create_smtp_creds_file(
file = "my_password_file",
user = "sender@gmail.com",
provider = "gmail", # Can be "outlook", "office365", "yahoo", or a custom SMTP
host = "smtp.gmail.com",
port = 587,
use_ssl = TRUE
)
Note: The code should be executed once to generate the password file. Upon execution, it will create a file in your current working directory with the name specified in the “file” argument. /
Step 2: R code for composing an Email message
Below code can be a part of the same R Analysis code (e.g. My_analysis.R)
# A regular data frame must be converted into HTML format to embed it properly in the email body.
library(knitr)
library(kableExtra)
<- kable(
html_table # Data frame to be converted into HTML
Analysis_df, format = "html", # Specify HTML format for the output
table.attr = "style='width:50%; border:1px solid black;'", # Set table styling
align = "c" # Center align the table content
%>%
) kable_styling() # Apply additional styling for enhanced appearance
# Define the email body with HTML content
<- paste0(
email_body "<p>Dear Recipient,</p>", # Salutation
"<p>Please find the requested analysis table below:</p>", # Introduction
# Embed the HTML table generated from the data frame. Table will appear in the email body
html_table, "<br><br>", # Add spacing between the table and closing remarks
"Best regards,<br>", # Closing phrase
"<b>XYZ Team</b>" # Signature with bold styling
)
# Compose the email with the defined body using markdown for rendering
<- compose_email(
email body = md(email_body) # Convert the email body into markdown format for compatibility
%>%
) add_attachment(
file = "C:/My_Path_to_file", # Path to the Excel file
filename = "Previous_Analysis_Table.xlsx" # Any file that needs to be sent as an attachment
)
Step 3: R code to send the email
%>%
email smtp_send(
to = c("receiver1@domain.com", "receiver2@domain.com"),
from = "sender@gmail.com",
subject = paste("Daily analysis report", sys_date),
credentials = creds_file("D:/Mention path to your password file") # Importing password file
)
Step 4: Automating the process to send email with Github
Create a private repository for securing your password file.
- Create a Private Repository - Log in to your GitHub account.
Click the + icon in the top-right corner and select New repository.
Fill in the repository name and description.
Select Private under Repository visibility.
Click Create repository.
Once repository is created, upload the Analysis R code file(e.g. My_analysis.R), Password file created in Step 1 (my_password_file), any other supporting files
- Enable Workflow Permissions
- Go to your repository’s Settings.
- Navigate to Actions in the left-hand menu.
- Scroll to Workflow permissions.
- Select Read and write permissions.
- Click Save.
- Create a folder named .github/workflows
- Add a workflow YAML file (e.g., main.yml)
- Note: The .github/workflows folder is necessary because it is where GitHub Actions looks for workflow YAML files that define automated tasks, such as CI/CD pipelines, to execute for your repository.
# Example of Yaml file store on private repository e.g. Email_from_R/.github/workflows/schedule-email.yml
# Workflow name
: Send email using R and blastula
name
# Trigger the workflow on a specific schedule (every weekday at 2:50 AM )
:
on:
schedule- cron: '50 2 * * 1-5'
:
jobs:
render-on: windows-latest # Set the environment where the job will run
runs
:
steps- uses: actions/checkout@v2 # Checkout the repository
- name: Set up R
: r-lib/actions/setup-r@v2 # Set up R environment
uses
- name: Install dependencies
: | # Run multiple commands
runinstall.packages(c("blastula", "quantmod", "dplyr", "purrr", "tidyr", "lubridate", "kableExtra"), type = "win.binary") # Required libraries for your analaysis R code
: Rscript {0} # Use Rscript to run the command
shell
- name: Run email script
: Rscript My_analysis.R # Execute the R script for sending emails
run
- name: Commit files
: | # Run multiple commands
run--local user.name github-actions
git config --local user.email "actions@github.com"
git config -am "commit on $(date)" # Commit with a message including the current date
git commit # Push changes to the main branch
git push origin main :
env: ${{ secrets.GITHUB_TOKEN }} # Access GitHub token from secrets
REPO_KEY: github-actions # Set up commit user username
Conclusion:
Using Blastula with R and GitHub streamlines email automation efficiently and cost-effectively. This setup eliminates dependencies on additional software or paid services, making it a powerful tool for automating your daily workflows.