Say hello to the MobileTrigger package. With it, you can run R scripts, models, and reports from a mobile device using e-mail triggers. In its debut offering, version 0.0.31 of the package provides List and Run trigger functions. The list function allows you to see available scripts, models, and reports. Complimenting list, the run function allows you to launch and interact with R while you're traveling and cannot be at your desk to run R directly. In this post, we'll cover MobileTrigger installation and setup. Follow-up articles will detail how to set up scripts, reports, and models for mobile execution and interaction.
The Concept
In Running Remote R Scripts with Mobile Devices using E-mail Triggers, I showed you how a mobile device could be used to trigger an R script back at your home or office. In a nut shell, The process looked like this:
- Write a R script on the remote machine that performed an operation and e-mails the results back to your mobile device using the mailR package.
- Create a special batch file that calls “Headless R” and your target R script.
- Set your desktop e-mail client to launch the special batch file when an e-mail having the subject “Hey R – Send Update” is received from your mobile device
The MobileTrigger Package
Having the ability to launch a single R script remotely is nifty. But, most of us need to run many R scripts, models, and reports during the day.
hmmm… How to handle this?
To work more generally with R via e-mail, I started working on the MobileTrigger package which is now on CRAN. The package allows for basic two-way communication between your mobile device and R. As discussed above, communication relies strongly on e-mail client rules, batch files, and headless R. The graphic below summarizes the core workflow of the MobileTrigger package.
Package Note: At this time, the MobileTrigger package only works for Windows users who read their e-mail with either Outlook or ThunderBird. I'm confident a solution could be made for Mac and Linux environments. If you would like to contribute to the project, please visit the github repository for the MobileTrigger package.
Now that we have oriented ourselves, let's install and setup the MobileTrigger package.
Objectives: Install and Setup MobileTrigger
Install MobileTrigger
Mobile triggers can be installed from CRAN or GitHub using the following code:
1 2 | install.package("MobileTrigger") # CRAN devtools::install_github("kenithgrey/MobileTrigger") # GitHub |
Planning Your MobileTrigger Setup
There is a lot of communication going on in the MobileTrigger package. Specifically communication needs to happen between the following three systems: your Mobile, R, and your desktop e-mail client. You can do this using the same e-mail address for all systems, a different e-mail address for each system, or a hybrid as detailed below:
MobileTriggers Uses One E-mail Address
1 | MobileEmail <- RsEmail <- MailClientEmail <- "Your.Email@somewhere.com" |
The benefit to using a single e-mail address is that you only need SMTP server settings for that one e-mail address.
MobileTriggers Uses Two E-mail Address
1 2 | MobileEmail <- "Email@MobileDevice.com" RsEmail <- MailClientEmail <- "Your.Email@somewhere.com" |
For this you will need SMTP settings for both e-mail addresses.
MobileTriggers Uses Three E-mail Address
1 2 3 | MobileEmail <- "Email@MobileDevice.com" RsEmail <- "For.R@R-email.com" MailClientEmail <- "Your.Email@somewhere.com" |
For this you will need SMTP settings for both R and the Mobile e-mail addresses.
In the next sections that follow we are going to use the three e-mail address approach. If you understand how to work with three email addresses than choosing to setup the mobileTrigger package using the 1 or 2 e-mail address options is trivial.
MobileTrigger File and Folder Setup
To get us rolling with the MobileTrigger package, we are going to start with the SetupWindowsTrigger() function. The function creates most of the files you need to facilitate communication between R and your mobile device. To use SetupWindowsTrigger(), you need to know the e-mail sever details you want to use for R to send messages to your mobile device. Please make sure you have the following information on hand:
- SMTP server for outgoing e-mail
- The SMTP server port
- Username
- Password
Other pieces of information you will need for the SetupWindowsTrigger() function include the
- “root trigger path”: The root trigger path is where MobileTrigger will conduct its business. It will also be the home of the scripts, models, and reports you plan to use with MobileTrigger. I recommend you put it some place simple like “c:/trigger” if you can.
Note: At this time spaces in the trigger path is poorly supported. Recommend that you keep the trigger path simple (e.g., “c:/trigger”)
- Mobile E-Mail Address: The package uses this to send results to your mobile device. Specify this e-mail address using the function’s Mail.To argument.
- R’s Sending E-mail Address: R will need a way to communicate out using the mailR package. You may want to make an e-mail address like R@mydomain.com. Specify this e-mail address using the function’s Mail.From argument
Below is an example call to the SetupWindowsTrigger() function showing how all this information gets used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | require(MobileTrigger) ################################################ # Define path & E-mail Addresses # ################################################ path <- "c:/trigger" MobileEmail <- "Email@MobileDevice.com" RsEmail <- "For.R@outlook.com" MailClientEmail <- "Your.Email@somewhere.com" ################################################### # R's Email Outlook SMTP Example (uses tls= TRUE) # ################################################### SetupWindowsTrigger( path = path, Mail.To = MobileEmail, Mail.From = RsEmail, SMTP.Settings = list(host.name = "smtp.office365.com", port = 587, user.name = RsEmail, passwd = 'password', tls = TRUE) ) |
Getting all the SMTP server information setup correctly can be challenging, particularly if you are using a webmail provider like gmail, yahoo, hotmail etc. If you are having trouble, check out my post on setting up webmail communication with the mailR package. Lastly, make sure you update the SetupWindowsTrigger function call with your information.
After Running SetupWindowsTrigger
After running the SetupWindowsTrigger function, take a moment to examine what's in the root trigger path. Below is a screenshot of what mine looks like with some descriptions.
As you can see from the screenshot above, the folder contains e-mail input files, a series of batch and R files for headless R operations, and some folders to put your codes depending on what you're trying to accomplish. All these files work together to facilitate communications between R and your mobile device.
If your trigger folder is written and you have a basic understanding of the files and folders, let's start setting up your e-mail client to work with the MobileTrigger framework.
Setting up the e-mail client
This section will help you get your desktop e-mail client (Outlook or ThunderBird) ready to work with the MobileTrigger package. This is the most involved part of the setup, but once you're done it should be trouble free.
ThunderBird
There are two ways to setup ThunderBird with the MobileTrigger framework, the fast way and the slow way. In general the process goes like this:
- Install FiltaQuilla Plugin
- Adjust FiltaQuilla options so the “Save Message as File” option to ON
- Setup ThunderBird Rules: The Fast Way or the Slow Way
- Test Communication
In the next few sections, we'll discuss the ThunderBird setup in detail. We'll start with the fast way; then I'll show an example of the slow way. Let's get started by installing the FiltaQuilla add-on.
Install FiltaQuilla add-on
We need to install a plugin called FiltaQuilla, so we can make e-mail filters in ThunderBird. To install the plugin:
- Open ThunderBird and let everything sync-up
- Press (ALT + t) on your keyboard. At the top of the ThunderBird window, the tools menu should appear
- Click the “Add-ons” menu item. This will open a new tab within ThunderBird called the “Add-ons Manager”.
- Click “Extensions” in the left-hand panel to open the extensions screen. Use the search box in the top-right corner to search for FiltaQuilla
- Install it
- Restart ThunderBird when you’re done.
- After restarting, press (ALT + t) again. This time select “Add-on Options” → “FiltaQuilla”. In the dialogue window, make sure the “Save Message as File” option is checked. Press OK. (see image below)
FiltaQuilla is now installed and ready. Close ThunderBird
Setup ThunderBird Rules: The Fast Way
This process takes me about 2 minutes. That being said, I've had a lot of developer practice. Anyway, the method we are about to do is much faster than the “slow” ThunderBird method or the Outlook method. Why? Because Filtaquilla's e-mail rules are in a nice editable text file called:
msgFilterRules.dat
- Make sure ThunderBird is closed
- Find the file msgFilterRules.dat buried deep in your windows home directory. For me, the path looked a little something like this:
C:/Users/[username]/AppData/Roaming/ThunderBird/Profiles/wrvfw5me.default/ImapMail/imap.ionos.com/msgFilterRules.dat
The highlighted portion of the path above is likely to be a little different for your local system, but the above should give you an indication of what the path looks like.
- Open the msgFilterRules.dat file in your favorite text editor. Initially, I had these two lines of text:
1 2
version="9" logging="no"
- If you already have filters working in your environment, backup the file before experimenting with it. Mistakes happen!
- In R, run the WriteThunderBirdFilters() function from the MobileTrigger package:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
require(MobileTrigger) ################################################ # Define path & E-mail Addresses # ################################################ path <- "c:/trigger" #your root trigger path MobileEmail <- "Email@MobileDevice.com" RsEmail <- "For.R@outlook.com" MailClientEmail <- "Your.Email@somewhere.com" ################################################ # Write the ThunderBird Filters # ################################################ WriteThunderBirdFilters( path = path, sent.from = MobileEmail )
After running this file, a new msgFilterRules.dat will be written to your root trigger path (in our examples the root trigger path has been c:/trigger).
- If you just installed FiltaQuilla for the first time, you can overwrite the existing msgFilterRules.dat file with the new one generated by R. If you have existing rules, copy the text from the R generated file from line 3 onward. Paste-append that text to the end of your existing msgFilterRules.dat using a text editor. Save and close.
- Open ThunderBird
- Press (ALT + t) to open the program tools menu and select the “Message Filters” menu item. The dialogue box that appears should show 6 rules as illustrated below:
- To check your setup, skip down to the Communication Testing section.
Setup ThunderBird Rules: The Slow Way
If you're in this section, you have FiltaQuilla installed, ThunderBird is presently closed, and you have elected to setup the rules using the slow way. This will take about 15-20 minutes. If you don't want to set up the rules this way, go to the Fast Way section.
Alright, the slow way.
Example List Script Rule
- Open ThunderBird
- Go to your root trigger path. In our running example, this would be c:/trigger.
- Run the “starterMessage.bat” file. This will launch headless R, and use the e-mail settings you’ve provided when you built the folder to send 6 messages with different subjects. Specifically, “Hey R – List Scripts”, “Hey R – Run Scripts”, “Hey R – List Models”, “Hey R – Run Models”, “Hey R – List Reports”, “Hey R – Run Reports”.
- When you receive the messages, copy the subject line from the “Hey R – List Scripts” email.
- Press (Alt + t), to open the ThunderBird Tool Menu, then click “Message Filters” and Press “New”. The following screen will appear.
- Name the rule “R List Scripts”, and specify a rule that responds to two conditions
- subject line is “Hey R – List Scripts”
- message is from your mobile e-mail address
- Next, set the rule action to launch the ListScripts.bat file in your trigger root folder. In our examples, we’ve been using c:\trigger. The resulting e-mail rule should look like the screenshot below. Other list types will look similar:
- After pressing OK, restart ThunderBird
- Test: Send an e-mail to your desktop with the subject line “Hey R – List Scripts” from your mobile device to make sure the batch file gets launched. You should see a command prompt open briefly with some R output. The message that returns to your mobile, will indicating “No Scripts in Path”. This is expected
We just setup the “List Script” trigger. Now, we need to setup the “Run Script” e-mail trigger.
Example Run Script Rule
- Copy the subject line from the “Hey R – Run Scripts” message.
- Open the FiltaQuilla Rule Manager using (Alt + t) → “Message Filters”
- Setup the run script rule as shown in the image below. Notice the filter criteria is the same as before, but the action has two operations:
- write message to file in the root trigger path (C:/trigger)
- Launch RunScripts.bat in the root trigger path
Completed rule should look like this:
- Press OK
- Restart ThunderBird
- Test: Send an e-mail to your desktop with the subject line “Hey R – Run Scripts” from your mobile device. Make sure the batch file gets launched. During the process, you should see a command prompt open briefly with some R output. Shortly after, you should get an e-mail message indicating “No Scripts in Path” sent back to your mobile. This is expected
If the first two triggers appear to be working as expected, congratulations! You made it through a very long process. To finish the process, grab a coffee, put on some upbeat music, and repeat the above procedure on the last 4 triggers listed in the table below as appropriate.
Rule Name | Filter Action | Filter Criteria |
---|---|---|
List Scripts | Launch: [Trigger Path]/ListScripts.bat | Subject == ‘Hey R – List Scripts’ From == ‘your.mobile.email@mobile.com’ |
Run Scripts | 1. Save Message as File to [Trigger Path]/ 2. Launch: [Trigger Path]/RunScripts.bat |
Subject == ‘Hey R – Run Scripts’ From == ‘your.mobile.email@mobile.com’ |
List Models | Launch: [Trigger Path]/ListModels.bat | Subject == ‘Hey R – List Models’ From == ‘your.mobile.email@mobile.com’ |
Run Models | 1. Save Message as File to [Trigger Path]/ 2. Launch: [Trigger Path]/RunModels.bat |
Subject == ‘Hey R – Run Models’ From == ‘your.mobile.email@mobile.com’ |
List Reports | Launch: [Trigger Path]/ListReports.bat | Subject == ‘Hey R – List Reports’ From == ‘your.mobile.email@mobile.com’ |
Run Reports | 1. Save Message as File to [Trigger Path]/ 2. Launch: [Trigger Path]/RunReports.bat |
Subject == ‘Hey R – Run Reports’ From == ‘your.mobile.email@mobile.com’ |
After you get all the email rules in, you'll want to test to make sure everything triggers as planned. You may have been doing this as you setup each trigger. If so, move on to the next steps section, otherwise skip down to the Communication Testing section.
Outlook
Setting MobileTrigger up with Outlook is going to take about 20 minutes. The process goes roughly like this:
- Tweek the Windows Registry
- Open Outlook
- Setup VBA Scripts & Enable Macros
- Restart Outlook
- Send Starter Messages
- Make the Rules
- Address Outlook Bug
- Test Communication
Registry Tweak
- Press the (Windows Key + R): This will open the run dialogue box
- In the “open” text box type: regedit
- You’ll get an “are you sure you want to use this application” warning from windows. Press OK…
- The registry editor should look like this:
Cool we got the registry editor open. Here are the changes we need to make
- In the left-hand panel drill down to:
Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook - Make a new folder (key) called “Security” by right clicking on the Outlook folder and selecting “New”→“Key”
- Enter the new “Security” folder. Full path should now be:
Computer\HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Outlook\Security - Create a new DWORD(32bit) value in the right panel by right clicking, call it “EnableUnsafeClientMailRules”
- Double click on EnableUnsafeClientMailRules and set the data value to 1.
- Should look like this when you are done:
All done.
Setup the Visual Basic Macros
- In the root path for MobileTrigger, open the file OUTLOOK.txt in a text editor and copy the text. The contents of this file is visual basic for applications (VBA) code. We need this to setup our email rules.
- Open Outlook
- On the program menu find the “Tell me what do search box”
- Search for “Visual Basic Editor” and open it. A new window should popup
- Find the “thisOutlookSession” module circled in the screenshot below and double click. Likely an empty window will appear. See example image below
- Paste in the VBA code you copied form the OUTLOOK.txt file (see step 1).
- Save and close the Visual Basic Editor
Enable Macros
If you don't enable macros, the MobileTrigger package won't work with Outlook. If the security risk is too great for you, consult your IT department or perhaps consider using ThunderBird instead.
- Go to “File” → “Options”
- Select the Trust Center
- Click the Trust Center Settings (a new window will appear)
- Select the Macro Settings Option
- Adjust the macro settings to “Enable All Macros”. Note Notification of all macros will work but only within a given Outlook session.
- Press OK.
- Restart Outlook.
Starter Messages
- Run the starterMessages.bat file in your root trigger path
- 6 messages should appear in your Outlook e-mail box.
Setup the Rules
- Right Click on the e-mail with subject “Hey R – List Reports”
- Go to Rules → “Create Rule…”
- Select advanced from the Create Rule Dialogue Box
- First screen – Set rule conditions
- Select the “From [Your Mobile e-mail]” and
- With “Hey R – List Reports” in the subject
- Press Next
- Second screen – set the action to do if the previous conditions were met
- In the Step 1: Select action box, select “run a script”. This option won’t be available if you haven’t made the windows registry change discussed above.
- In the Step 2: Edit the Rule Description… Click the words “a script”
- A “Select Script” dialog box will appear. Select the VBA macro for listing reports, and press “OK”
- Press “Next” to move to the third screen
- Third screen – set exceptions to the rules. We aren't going to worry about this right now.
- Press Next
- Last Screen – Finish Rule
- Step 1: Give your Rule a name
- Step 2: Make sure the rule is turned on
- Press Finish
Address the Outlook Bug
If you try to test the rule at this point, it may not work because of an Outlook bug. This bug tripped me up for several hours. So hopefully this saves you some time.
- On the Home Ribbon, click “Rules” → “Manage Rules & Alerts”
- A Dialogue box will open, and show your new rule for R to list reports. To the right of the rule name you will see “(For Other Computer)”. For whatever reason this causes problems.
- To get rid of it, click the wrench and screw driver at the far right (see picture below).
- The Rule Wizard will open, Toggle OFF then ON “on this computer”
- Press Finish
- You should no longer see the “(For Other Computer)” in your rule list.
- Press OK
- Check that the List Report Rule is working by sending a test e-mail to Outlook with the header “Hey R – List Reports”. Make sure its from the right e-mail address or the rule won’t run.
If the test works, repeat the above steps on the other 5 triggers. Here is a complete list.
Rule Name Use Script Filter Criteria List Scripts OutlookRule.ThisOutlookSession.ListReports Subject == ‘Hey R – List Reports’
From == ‘your.mobile.email@mobile.com’Run Scripts OutlookRule.ThisOutlookSession.RunReports Subject == ‘Hey R – Run Reports’
From == ‘your.mobile.email@mobile.com’List Models OutlookRule.ThisOutlookSession.ListScripts Subject == ‘Hey R – List Scripts’
From == ‘your.mobile.email@mobile.com’Run Models OutlookRule.ThisOutlookSession.RunScripts Subject == ‘Hey R – Run Scripts’
From == ‘your.mobile.email@mobile.com’List Reports OutlookRule.ThisOutlookSession.ListModels Subject == ‘Hey R – List Models’
From == ‘your.mobile.email@mobile.com’Run Reports OutlookRule.ThisOutlookSession.RunModels Subject == ‘Hey R – Run Models’
From == ‘your.mobile.email@mobile.com’Once you have everything setup, move on to the Communication Testing section
Communication Testing
Congratulations, if you've made it to this section I assume you have all the rules setup in either Outlook or ThunderBird. Perhaps, you've tested your communications as you set up each rule. If you know the rules are working, skip this section and move on to this post's Summary. If you still need to test your setup, here is an R script to help you through the process. The script is organized as follows.
- Load the Required Libraries
- Run the trigger tests
The testTrigger() function in the script below will make requests to List and Run scripts, models, and reports that don't exist currently in your setup. This is fine. We are checking to make sure communication is working. In follow-up posts, you'll get to see these modules in action.
To test your triggers and communications, start by running the “list” triggers line [9]. There is a 15 second delay between each message sent. As the requests roll in, your triggers should activate, and you should start seeing e-mail from R indicating “no” model, script, or report in path. If these are working, move on to test each set of Run Triggers (Model, Script, and Report). Remember to update the Mail.To and Mail.From for your situation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Load Libraries ---------------------------------------------------------- require(mailR) require(MobileTrigger) ################################################ # Define path & E-mail Addresses # ################################################ path <- "c:/trigger" #your root trigger path MobileEmail <- "Email@MobileDevice.com" RsEmail <- "For.R@outlook.com" MailClientEmail <- "Your.Email@somewhere.com" ################################################ # Test Communication # # - Test Each of the TestWhat Conditions # ################################################ testTriggers( TestWhat = "Lists", #TestWhat = "RunModels", #TestWhat = "RunScripts", #TestWhat = "RunReports", path = path, Mail.To = MailClientEmail, # Client Email Address Mail.From = MobileEmail, # Simulate Mobile Mail.From.SMTP.Settings = list(host.name = "Mobile SMTP Server Settings", port = [mobile SMTP port number], user.name = MobileEmail, passwd = 'Mobile Password', tls = TRUE) )
If all “List” and “Run” modules are communicating, MobileTrigger is setup and ready for use.
Next Steps
If MobileTrigger is working with your e-mail client, the next step is to bring in your models, scripts, and reports. We will cover each case in a series of brief follow-up posts. So, stay tuned!
Summary
You made it to the end. Yes, I know setting up the e-mail client is a pain. However, if you've made it this far you've got the basic idea of how the MobileTrigger package works. As well, you should have working e-mail communications between your mobile device and R on a remote desktop at your home or office. In a series of brief upcoming posts, I will show you how to integrate your reports, models, and scripts.
If anyone wants to support the project by making the Outlook setup easier or setting up capabilities for Linux or Mac, please visit the MobileTrigger GitHub Project
Other Articles from r-bar.net
Running Remote R Scripts with Mobile Devices using E-mail Triggers
WooCommerce Image Gallery | Step by Step, Automate with R
XmR Chart | Step-by-Step Guide by Hand and with R
Windows Clipboard Access with R
One Comment
Pingback: