Display a Time Clock in the R Command Line

Permanently display a time clock in the R command line

One option is to have a .Rprofile file in the ~/ (usually "C:/Users/me/Documents" in windows), and add the following into it.
It will show time as soon as you do something on the console.

.First <- function(){

h <- taskCallbackManager()
h$add(function(expr, value, ok, visible) {
options("prompt"=format(Sys.time(), "%H:%M:%S> "));
return(TRUE) }, name = "simpleHandler")
}

I think you could do this in the Rprofile.site in your
"C:\Program Files\R\R-x.x.x\etc" as well. As noted by @r2evans, this seems like a bad idea.

Time stamp at R prompt possible?

This will do it:

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

I forget how to make it update though. I'll try to check when I get a sec...

Ahh found it:

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

R profile, setting prompt to the date

This is simple:

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

Hook into Rgui console?

Yes, though it is independent of the GUI as just uses the callback mechanism as for example in the quesion on R: Display a time clock in the R command line.

R language - timer

In the tcltk2 package is the tclTaskSchedule function (and others) that could be used to do what you want. Be warned that this will usually violate the idea of functions not having side effects and you can really mess things up if the scheduled function uses any of the same objects that you are working with. It would be fine if the task just read data into a local variable and plotted the latest version (just make sure that it plots to the correct graphics device and does not mess up something else you are working on).

If you just want something to update on a regular basis you could use a repeat loop (or while) and Sys.sleep to wait the given time, then do whatever you want. You would not be able to use that R session for anything else, but you can easily have multiple R sessions running at the same time so that would not prevent you from working in another R session.

How do I measure execution time of a command on the Windows command line?

If you are using Windows 2003 (note that windows server 2008 and later are not supported) you can use The Windows Server 2003 Resource Kit, which contains timeit.exe that displays detailed execution stats. Here is an example, timing the command "timeit -?":

C:\>timeit timeit -?
Invalid switch -?
Usage: TIMEIT [-f filename] [-a] [-c] [-i] [-d] [-s] [-t] [-k keyname | -r keyname] [-m mask] [commandline...]
where: -f specifies the name of the database file where TIMEIT
keeps a history of previous timings. Default is .\timeit.dat
-k specifies the keyname to use for this timing run
-r specifies the keyname to remove from the database. If
keyname is followed by a comma and a number then it will
remove the slowest (positive number) or fastest (negative)
times for that keyname.
-a specifies that timeit should display average of all timings
for the specified key.
-i specifies to ignore non-zero return codes from program
-d specifies to show detail for average
-s specifies to suppress system wide counters
-t specifies to tabular output
-c specifies to force a resort of the data base
-m specifies the processor affinity mask

Version Number: Windows NT 5.2 (Build 3790)
Exit Time: 7:38 am, Wednesday, April 15 2009
Elapsed Time: 0:00:00.000
Process Time: 0:00:00.015
System Calls: 731
Context Switches: 299
Page Faults: 515
Bytes Read: 0
Bytes Written: 0
Bytes Other: 298

You can get TimeIt in the Windows 2003 Resource Kit. It's not available for direct download from the Microsoft Download Center, but one can still get it from the archive.org - Windows Server 2003 Resource Kit Tools.

How to show working directory in R prompt?

Because prompt option is really just a string, without any special directives evaluated inside (unlike shell prompt), you have to change it if you change working directory to get current working directory inside.

The solution you use seems the best to me. A bit hacky, but any solution will be as you want to implement something quite fundamental that is not supported by R itself.

Moreover, you don’t have to fear functions executing base::setwd under the hood, which would get your prompt out of sync with real working directory. That does not happen in practice. As Thomas pointed out in the comments, probably no base functions (other than source) call setwd. The only functions that do are related to package building and installation. And I noticed that even in source and usually in the other functions, setwd is used like owd <- setwd(dir); on.exit(setwd(owd)), so that the working directory is set back to original when the function finishes.



Related Topics



Leave a reply



Submit