6

Can someone explain to me, in simpler non-computer science term, what is the Callback features in R (taskCallback, getTaskCallbackNames, taskCallbackManager ... etc)? I have looked at the R help, but I find the information a bit too abstract.

What is it design to do, and how can a user make use of them?

If someone can explain the general concept (in R and other computer languages), and provide example in R, I would appreciated very much, since I never really understood. Does it have to do anything with recursive functions, or am I misguided by the name callback?

symbolrush
  • 7,123
  • 1
  • 39
  • 67
lalas
  • 763
  • 1
  • 7
  • 10

2 Answers2

5

I don't know much about R, so I can't delve into any R specifics here. That being said:

In general, in imperative, procedural and functional programming languages (and maybe in some other paradigms as well), calling a function will block until that function is finished, and pass out the function's result to the caller. This is typically is a good way to do things, however in some cases, we might have requirements that make this a less viable modus operandi.

For long running operations, we might not want to block the caller for so long. Depending on the environment we're in, the caller might not have the possibility to spawn another thread, or the possible number of threads might be too small to accomodate the required number of parallel calls, so doing long-running operations in this synchronuous way will give a very bad experience. JavaScript with its single-threaded model and frequent neccessity to make calls to a server is a typical example.

So the basic idea of Callbacks is, instead of having the called function return when the actual processing is complete, the caller passes in a Callback object (in OOP, in other paradigms something similar, e.g. a callback function, often anonymous, for functional programming). The called function will return immediately, freeing the calling thread to do other stuff. When the long-running process is finished, the Callback will be called and it is there that the caller can process the results given from the long-running process.

This schema can be generalized a bit, so not only will the callback called at the end of the processing, but also regularly while processing, providing some kind of status update, so the caller can e.g. display some feedback to the user (status bar, estimated time to completion, ...). Another common addition is a way for the caller to cancel the task while it is being processed.

That's the general principle. Maybe someone more knowledegable can fill in the details of how this applies to R and where R differs from this general description.

  • Thank you for such a high level explanation.. I do have a better understanding for it now... :) – lalas Aug 14 '12 at 04:53
4

Details and examples can be found here.

The basic idea does not deal with recursion, but rather with calling a function (or functions) for every top level commend issued to the R command line.

One use of this is in the TeachingDemos package, the txtStart and related functions use task callbacks to save a copy of each command and its resulting output to an external file creating a transcript of the interactive session.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • what is meant by "top level command"? Do you mean a function written in R, instead of C? I have see the examples, given in the link, but i failed to see its usage (sorry that is probably due to my ignorance, and not the article itself.. I am sure, it would make a lot of sense, if i could see why/how i could use these call back mechanism).. Perhaps, the example in the teaching Demo might provide a better illustration of concept.. Cheers for that.. :) – lalas Aug 14 '12 at 04:59
  • 1
    A top level command is what you type in at the command prompt, so `1+1` is a top level command, but a `for` loop that runs a bunch of commands would be a single top level command when typed at the command prompt. Try the example in the linked document and type different things in to see how often and when the counter changes, each change was a top level command. – Greg Snow Aug 14 '12 at 17:33