0

I am aware of how to add a timestamp during an R session using

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

as described in this answer.

How can I make this a permanent feature, so that RStudio always has this as the prompt?

Adam_G
  • 7,337
  • 20
  • 86
  • 148

1 Answers1

1

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.

kangaroo_cliff
  • 6,067
  • 3
  • 29
  • 42
  • Thanks! Do you know where that's located in OSX? – Adam_G Mar 22 '19 at 04:11
  • maybe just do `getwd()` after you start Rstudio? Also `setwd("~/")` should get you to "~/" as well. – kangaroo_cliff Mar 22 '19 at 04:12
  • @Adam_G `~/` is just the home directory; on a Mac that's usually something of the form `/Users/your_user_name` (just type `cd ~` and `pwd` to see the path). – Maurits Evers Mar 22 '19 at 04:27
  • 1
    You can find out where R thinks it should be with `normalizePath("~")`. – r2evans Mar 22 '19 at 04:32
  • 1
    @Adam_G : Please read the help page you've already been referred to: It contains these sentences: "The path of this file is taken from the value of the R_PROFILE environment variable (after tilde expansion). If this variable is unset, the default is ‘R_HOME/etc/Rprofile.site’, which is used if it exists (which it does not in a ‘factory-fresh’ installation). " – IRTFM Mar 22 '19 at 05:00
  • 1
    @Suren, putting configuration like this in the system-wide location such as within `c:/Program Files/R/` is not a good idea, for two reasons: (1) when you update R, it stops working; (2) if for any reasons there are other users on that machine, they are "forced" to use this as well. I know it's a backup recommendation, secondary to the `~/.Rprofile` location, but I still think it's a recommendation that flies against solid practices. – r2evans Mar 22 '19 at 05:21
  • 1
    @r2evans thanks a lot. I didn't think about it that way. – kangaroo_cliff Mar 22 '19 at 08:52
  • Thank you for all of this advice. I tried placing the code in `~/.Rprofile`, and then restarted Rstudio, but nothing changed. – Adam_G Mar 22 '19 at 20:57
  • @Adam_G try `1 + 1` in the console and see if the time appears or not. – kangaroo_cliff Mar 25 '19 at 01:55
  • Ok, it seems to work now. I give up. But thank you!! – Adam_G Mar 25 '19 at 18:24