kittygirl
kittygirl

Reputation: 2443

How to judge whether function completed in R?

I want to automatically beep() when function is completed(successful runned) or not completed(error)

Per this post,options(error = beep) can be used when function not completed.
addTaskCallback will be called each time a top-level task is completed.

Then,how to know function is completed?

Upvotes: 0

Views: 471

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76641

There is a package beepr, see also here:

beepr is an R package that contains one function, beep(), with one purpose: To make it easy to play notification sounds on whatever platform you are on. It is intended to be useful, for example, if you are running a long analysis in the background and want to know when it is ready.

See also this SO answer.

The following code will beep 5 times, one time per call to f. This is because beep() is in on.exit that, like the name says, executes its argument(s) on exit of the function.

f <- function(x){
  on.exit(beepr::beep())
  Sys.sleep(x)
}
g <- function(s) {
  for(i in 1:5) {
    f(s)
  }
}

g(1)

Upvotes: 1

Related Questions