Search code examples
rwindowscommand-linemessageboxr-taskscheduler

Scheduling an an R script that shows a popup/message box in case of an error on Windows


My aim is to check the http status of a website every 5 minutes and throw an alert message in case it is not 200. To keep it simple, I would like to discuss my question based on the piece of code given below.

library(httr)

a <- status_code(GET("http://httpbin.org/status/404"))

if (a == 404) system('CMD /C "ECHO Client error: (404) Not Found && PAUSE"', 
                 invisible=FALSE, wait=FALSE)

The last bit that begins with system found on

https://heuristically.wordpress.com/2013/04/19/popup-notification-from-r-on-windows/

and

Show a popup/message box from a Windows batch file

The lines above results in
enter image description here

This is a message box from C:\windows\SYSTEM32\CMD.exe poping up that says:

Client error:(404) Not Found

Press any key to continue...

Is there a possiblity to add Sys.time() along this message?

Using the taskscheduleR I scheduled the script above. To get help see:

http://bnosac.be/index.php/blog/50-taskscheduler-r-package-to-schedule-r-scripts-with-the-windows-task-manager-2

library(taskscheduleR)

myscript <- "the address of your r script"

taskscheduler_create(taskname = "myfancyscript_5min", rscript = myscript, 
                 schedule = "MINUTE", starttime = "11:20", modifier = 5)

In this case the message box I get is shown below. Note that this time it is without the message.

How can I get the message written when I run the script using the task scheduler?

enter image description here


Solution

  • You just need to edit the first part of the code. As advised in the comment we will make use of notifier:

    https://github.com/gaborcsardi/notifier

    In case you have issues with installing the notifier, I was only able to install it through the command below.

    devtools::install_version("notifier")
    

    Replace the first bit with the following:

    library(httr)
    
    library(notifier)
    
    a <- status_code(GET("http://httpbin.org/status/404"))
    
    if (a == 404) notify(
                     title = "404",
                     msg = c("Client error: (404) Not Found")
    
    )