48

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.

lmo
  • 37,904
  • 9
  • 56
  • 69
Shu
  • 611
  • 5
  • 5
  • 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 08 '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 08 '12 at 18:06
  • 2
    @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 08 '12 at 18:31

5 Answers5

52

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
07:25:48>

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • 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 08:48
  • Thanks Joris, Dirks solution is very helpful, just I am a bit greedy for more as someone would like me to provide time information of error reported in the R console. Wondering if it is possible. – Shu Nov 24 '10 at 01:13
  • 1
    @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
  • This doesn't work on my mac (running 2.14) in the GUI but it does work when R is launched as a terminal app. Any ideas why? – Dason Dec 12 '11 at 20:20
  • GUIs interfere with event loops, and hence muck up the callback scheme. – Dirk Eddelbuettel Dec 12 '11 at 20:34
  • Unfortunately, a callback that depends on a top level execution will not update with CR/LF, unlike the DOS prompt. This is where `tcltk2` has some neat stuff. – Iterator Feb 08 '12 at 18:33
  • How can I add this to something like my `.Rprofile` so it loads every time, in every project? – Adam_G Mar 19 '19 at 23:45
  • I had to assign the handler (and helper function it calls) to .GlobalEnv in .Rprofile - perhaps because I did this all in an if (interactive()) {} block. – Jack Wasey Apr 18 '19 at 22:04
19

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:

library(tcltk2)
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.

Community
  • 1
  • 1
Iterator
  • 20,250
  • 12
  • 75
  • 111
18

Here is an alternative callback solution:

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

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

John Colby
  • 22,169
  • 4
  • 57
  • 69
  • 1
    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 08 '12 at 18:33
3

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

options(prompt = paste(Sys.time(), ">"))

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.

Community
  • 1
  • 1
Chase
  • 67,710
  • 18
  • 144
  • 161
0

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.

Iterator
  • 20,250
  • 12
  • 75
  • 111
Daniel Standage
  • 8,136
  • 19
  • 69
  • 116