1

I found a great article (here) to successfully execute an R program through a combination of a .bat file and Windows (7) Task Scheduler command. Then I used the top rated answer to this question in order to send an email through R.

When I simply execute the R program in R, it emails beautifully. When I run the Windows Task Scheduler to execute the .bat file (which executes the R program), it will create the file that I want but it fails to email. I'm reaching out to this community to see if there are some ideas why the email will not send through the .bat method.

The code below creates a simple .csv file, writes it to my desired path, and then emails it out:

t <- "mission accomplished overnight!"
setwd("C:/Users/bshelto1/Documents/testtoremove1/scheduler_test")
write.csv(t, "mission_accomplished.csv", row.names = FALSE)

library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)

outMail[["To"]] <- "my_email@gmail.com"
outMail[["subject"]] <- "i hope this works"
outMail[["body"]] <- "mission accomplished!"
outMail[["Attachments"]]$Add("C:\\Users\\bshelto1\\Documents\\testtoremove1\\scheduler_test\\mission_accomplished.csv")

outMail$Send()

As I mentioned, when I run the above program in R, it works great (i.e., it creates the file and emails it with attachment).

The next part was creating the .bat file to execute the R program. The .bat code is below:

@echo off 
"C:\Users\bshelto1\Documents\R\R-3.2.0\bin\x64\R.exe" CMD BATCH C:\Users\bshelto1\Documents\testtoremove1\scheduler_test\mission_test.R

The final part was creating the Windows Task Scheduler command to completely automate the job. The R code is below:

recurrence  <- "once"
task_name <- "Test4"
bat_loc <- "C:\\Users\\bshelto1\\Documents\\testtoremove1\\scheduler_test\\task.bat"
time <- "07:52"

system(sprintf("schtasks /create /sc %s /tn %s /tr \"%s\" /st %s", recurrence, task_name, bat_loc, time)) 

I've actually narrowed the issue down to the .bat file. If I just run the .bat file, it creates the .csv file by the R program, but won't send the email. Any ideas what would be causing the .bat file to not execute the email portion of the R code?

Community
  • 1
  • 1
bshelt141
  • 1,183
  • 15
  • 31
  • You're running the x64 version of R through batch but probably x86 when you try it in R GUI (AFAIK COMCreate works only in x86)...So, try to replace x64 with x86 in your .bat file – digEmAll Apr 07 '16 at 12:05
  • @digEmAll thanks for the response. I tried replacing x64 with x86, but "The system cannot find the path specified" because I don't have an x86 path. I tried replacing x64 with the i386 path, but it returned the same results as my original (i.e., created file but not email). I'm wondering if I'll just have to change to another way of sending email through R... – bshelt141 Apr 07 '16 at 12:49
  • Yes sorry i386... so, I don't know... usually I use `\Rscript.exe --vanilla "myscript.R"` instead of `R.exe CMD BATCH...` can you try this (and check the console output)? – digEmAll Apr 07 '16 at 13:56
  • @digEmAll Using your recommendation resulted in this console message: "console output: Error in library(RDCOMClient) : there is no package called 'RDCOMClient' Execution halted Press any key to continue . . ." – bshelt141 Apr 07 '16 at 15:06
  • Mmh... I see 2 possibilities: 1 - you have different R versions installed on your machine and you're using one version (3.2.0) for the batch and another when you test the script. 2 - you have installed the package for a user that is not the same you are using when you launch the script through batch. For the second possibilities, you can install a package for all users by simply running R GUI / RStudio as administrator... – digEmAll Apr 07 '16 at 15:13

0 Answers0