April 1, 2014

How do you check the task completion in R?

When you run your analysis in R, how do you spent your time? Check the progress bar, or do another thing? I have divided that into three cases. The time which the analysis need is very short, short and long. At the same time, I selected R packages and functions which are suitable for each case.

Case 1. Very short

This case includes less than three minutes, which you can bare to behold the display. I think txtProgressBar function is suitable for this case. With this function, you can visulize the progress.

pb <- txtProgressBar()
for (i in 1:10) {
    Sys.sleep(0.5)
    setTxtProgressBar(pb, i/10)
}

2014-04-02 add

The function tkProgressBar of tcltk package would be also helpful. Please check it out the example of tkProgressBar. (Thanks to Mr. Hayashi!)

Case 2. Short

In this case, you have to endure from 5 to 15 minutes to check the results of the analysis. I am afraid that you would do another task in parallel. So the notice is needed. You can choose two types of the notice, pop-up or sound. To make a pop-up message in R, you can use tcltk package as follows.

library(tcltk)
Sys.sleep(5)
tkmessageBox(message = "Finished!", icon = "info", type = "ok")

Also you can use a sound notice with pingr package. ping() function of this package alarms you with 9 sounds. Other than built-in sounds, You can jingle any wave file.

# This package is not uploaded to CRAN
devtools::install_github("rasmusab/pingr")
library(pingr)
ping(5)

Case 3. Long

The final case is that it takes a long time to complete the task. In such a case, you would be away from your display. Thus you will recieve the notice with your mobile device, for example, iPhone, Androids and tablet PCs. There are two ways, E-mail and push notifications from the application for mobile phones.

You can send E-mail from R with mailR package (It requires Java). If you use gmail, the code is like this.

library(mailR)
sender <- "sender@gmail.com"
recipients <- c("recipients@gmail.com")
# You need to get your application specific password(see below)
# https://support.google.com/accounts/answer/185833
email <- send.mail(from = sender,
                   to = recipients,
                   subject="Subject of the email",
                   body = "Body of the email",
                   smtp = list(host.name = "smtp.gmail.com", 
                               port = 465, 
                               user.name = "yourname@gmail.com", 
                               passwd = "your application specific password", ssl = TRUE),
                   authenticate = TRUE,
                   send = TRUE)

If you want to send a HTML mail, you can use EasyHTMLReport package.

A push notice is a convenient way as well. With pushoverr package and pushoverAPI, you can receive push notifications when the task is completed. Other than R, pushoverAPI can be joined with various components, such as IFTTT, Github, etc (check here for other services).

After the registration and acquiring your USER KEY and your APP TOKEN, you can send a notice as follows.

library(pushoverr)
yourkey <- "XXXXXX"
yourtoken <- "XXXXXX"
pushover(message = "Mission complete!!!", user = yourkey, token = yourtoken)

To summarise, R offers you various notice ways for your aims. If you use other ways, please let me know the one.

2 comments:

  1. How would I tell R to apply these to whatever process I'm running?

    For example, if I am parsing a huge data.table, and it takes 10+ minutes to do, how will R know to run the code to send me an email when the process completes?

    ReplyDelete
    Replies
    1. As for me, I just add notifications after my process.
      How about the following?

      library(data.table)
      library(pingr)
      dat <- fread(sampledata) # This line can be replaced with your operation
      ping() # This line can be replaced with other notifications

      Delete