Take the 2-minute tour ×

I wonder if there is a way to display the current time in the R command line, like in MS DOS, we can use "Prompt $T $P$G" to include the time clock in every prompt line. Something like options(prompt=paste(format(Sys.time(), "%H:%M:%S"),"> ")) will do it, but then it is fixed at the time it was set. I'm not sure how to make it update automatically.

share|improve this question
None of the current callback-based answers does the same thing as the MS DOS prompt, because they depend on the execution of a top-level command. If one simply presses CR/LF in the console, it will not update the time, while this will be updated in MS DOS. –  Iterator Feb 8 '12 at 17:50
Where I'm sitting I can see the time on my screen clock, my wristwatch, my trusty old Palm VX, and my phone. Do you really need another reminder of your increasing mortality? :) Is it really just for timestamping your activity? –  Spacedman Feb 8 '12 at 18:06
@Spacedman Yes, that's what I'd do. Sometimes I run a command from the console, and would like to know when it ended, if I've stepped away. The other methods do that reasonably well, but then I still have to check the clock time (or execute a new command). I have a lot of profiling, logging, messaging, etc., so this prompt thing could be superfluous, but what the heck. :) In any case, I thought it best to solve the question that was asked. –  Iterator Feb 8 '12 at 18:31

5 Answers 5

Chase points the right way as options("prompt"=...) can be used for this. But his solutions adds a constant time expression which is not what we want.

The documentation for the function taskCallbackManager has the rest:

R> h <- taskCallbackManager()
R> h$add(function(expr, value, ok, visible) { 
+     options("prompt"=format(Sys.time(), "%H:%M:%S> ")); 
+             return(TRUE) }, 
+     name = "simpleHandler")
[1] "simpleHandler"
07:25:42> a <- 2

We register a callback that gets evaluated after each command completes. That does the trick. More fancy documentation is in this document from the R developer site.

share|improve this answer
Ooops, just fixed the URL to Duncan TL's write-up. –  Dirk Eddelbuettel Nov 19 '10 at 14:06
@Dirk : Wonderful, thx for the pointer to that function. Yet, this solution still has a -minor- bug in it: when you have an error, it doesn't update the time. Try f <- function() { Sys.sleep(1) ; stop() } - and then - f() . Not that I would have known how to do that though... –  Joris Meys Nov 19 '10 at 16:12
Thanks Dirk and Joris! @Joris: could you elaborate a little further on updating the time when there is an error. I tried function() { Sys.sleep(1) ; stop() }, but it did not seem to work. –  Shu Nov 22 '10 at 23:54
@Shu : if you use Dirks solution and afterwards you try that function I gave, you see that the time is not updated. The callback does not get evaluated when the command issues an error. But that's a very tiny minor inconvenience I just liked to mention for hairsplitting sake ;-) –  Joris Meys Nov 23 '10 at 8:48
@Shu : If you need the time between the command is given and the error is reported, use system.time(). It tells you when exactly the timing stopped. –  Joris Meys Nov 24 '10 at 12:41

Here is an alternative callback solution:

updatePrompt <- function(...) {options(prompt=paste(Sys.time(),"> ")); return(TRUE)}

This works the same as Dirk's method, but the syntax is a bit simpler to me.

share|improve this answer
Unfortunately, neither is precisely like the DOS prompt, which would be updated even when the return key is pressed. The callbacks only wait for a top level execution. To learn how to do it via a clock trigger, I created a solution with the tcltk2 package. –  Iterator Feb 8 '12 at 18:33

None of the other methods, which are based on callbacks, will update the prompt unless a top-level command is executed. So, pressing return in the console will not create a change. Such is the nature of R's standard callback handling.

If you install the tcltk2 package, you can set up a task scheduler that changes the option() as follows:

tclTaskSchedule(1000, {options(prompt=paste(Sys.time(),"> "))}, id = "ticktock", redo = TRUE)

Voila, something like the MS DOS prompt.

NB: Inspiration came from this answer.

Note 1: The wait time (1000 in this case) refers to the # of milliseconds, not seconds. You might adjust it downward when sub-second resolution is somehow useful.

share|improve this answer

You can change the default character that is displayed through the options() command. You may want to try something like this:


Check out the help page for ?options for a full list of things you can set. It is a very useful thing to know about!

Assuming this is something you want to do for every R session, consider moving that to your .Rprofile. Several other good nuggets of programming happiness can be found hither on that topic.

share|improve this answer
Actually, not quite. You get the constant time expression of when options("prompt"=...) was set. You need a callback as shown in my answer. –  Dirk Eddelbuettel Nov 19 '10 at 13:32
It's the solution OP gave and didn't like. But the reference to the Rprofile is useful. –  Joris Meys Nov 19 '10 at 15:18

I don't know of a native R function for doing this, but I know R has interfaces with other languages that do have system time commands. Maybe this is an option?

Thierry mentioned system.time() and there is also proc.time() depending on what you need it for, although neither of these give you the current time.

share|improve this answer

Your Answer


