bnosac / taskscheduleR

Schedule R scripts/processes with the Windows task scheduler.
330 stars 72 forks source link

Trying to send automate process of sending an email in R #62

Closed jonathantaylor7 closed 5 years ago

jonathantaylor7 commented 5 years ago

I'm trying to use RStudio to run an automated email out. I'm using taskscheduleR to do this but it doesn't seem to be working for me. The data code is here: ExampleRStudioCode.rmd

Summary of Car Speed and Distance

summary(cars)

Plot of Car Speed and Distance

You can also embed plots, for example:

plot(pressure)

The email code is here: TestEmail.rmd

require(knitr)
require(markdown)

knit("ExampleRStudioCode.Rmd")
markdownToHTML('ExampleRStudioCode.md', 'ExampleRStudioCode.html', options=c("use_xhml"))
system("pandoc -s ExampleRStudioCode.html -o ExampleRStudioCode.pdf")

library(RDCOMClient)
OutApp <- COMCreate("Outlook.Application")
outMail = OutApp$CreateItem(0)
outMail[["To"]] = "(contains my email)"
outMail[["SentOnBehalfOfName"]] = "(contains my email)"
outMail[["subject"]] = "This is a test for sending emails in R"
outMail[["body"]] = "Hope this works. Let me know if you see any errors. Working on some sort of automation of this now. - Jonathan"
outMail[["Attachments"]]$Add('ExampleRStudioCode.pdf')
outMail$Send()

The taskscheduleR code is here: AutomatedSend.Rmd


##Run everyday at 10am starting 1/17/2019

library(taskscheduleR)
myscript <- 'C:\\Users\\jonathantaylor\\Documents\\TestEmail.rmd'
taskscheduler_create(taskname = "AutomatedEmailTest", rscript = myscript, 
                     schedule = "ONCE", starttime = format(Sys.time() + 62, "%H:%M"))

Any ideas why it might not be running? The data and email scripts both run and work. It's just the automation where all I got is a quick flash of a black box and that's it. Hopefully there is an easy fix to this that I am missing.

jwijffels commented 5 years ago

myscript should be a .R file look to the log in the location of your .R file

jonathantaylor7 commented 5 years ago

Thanks for responding so quickly. I changed all of my files to .R rather than .Rmd and I'm still seeing this issue.

I also looked at the log and this is what I got:

system.file("extdata", "TestEmail.log", package = "taskscheduleR") [1] ""

jwijffels commented 5 years ago

Readme says

When the task has run, you can look at the log which contains everything from stdout and stderr. The log file is located at the directory where the R script is located.

jonathantaylor7 commented 5 years ago

Makes sense. Missed that one. So now I go into the log file and I'm seeing where it's stopping at:

Loading required package: knitr Loading required package: markdown Error in file(file, ifelse(append, "a", "w")) : cannot open the connection Calls: knit -> cat -> file In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file 'ExampleRStudioCode.txt': Permission denied Execution halted

Seems to be a permissions issue with the folder I have this file in. Is it maybe something other than that?

jwijffels commented 5 years ago

Seems to me your own code contains errors. Debug them. This is unrelated to the taskscheduleR package. Please ask such questions related to debugging your own code somewhere else like on stackoverflow.