Tuesday, May 15, 2012

R-startup scripts

If there are some things you use all the time in R, you might not want to have to load them specifically every time. That is what ~/.Rprofile is for. As a start, have a look at R-bloggers: Customizing your .Rprofile and Customizing R: startup script. Then the question is what to put in your startup-script.

cat("Good morning Dr. Chandra.\n\n", sep = "") is an excellent choice.

As an alternative:
library(lubridate)
if (hour(Sys.time()) < 12)
  cat("\nGood morning Dr. Chandra!\n\n", sep = "")
if ((hour(Sys.time()) >= 12) & (hour(Sys.time()) < 18))
  cat("\nGood afternoon Dr. Chandra!\n\n", sep = "")
if (hour(Sys.time()) >= 18)
  cat("\nGood evening Dr. Chandra!\n\n", sep = "")
is more probable to be correct in a linguistic sense.

Then there are other important things. Like setting the language to English. For some reason R thinks I want it to speak German, even though nothing else on my system is in German. I have given up trying to find out how to fix it properly, so I use: Sys.setenv(LANG = "EN")

As they point out elsewhere though, you shouldn't put too many things in that you actually use for data analysis. That would make your code difficult to follow. It might be anyway, as mine is, but at least all libraries and such are loaded in the R-directory of the specific project. However, I do have a loading option for a .Rbasicfunctions.R:
if(file.exists("~/.Rbasicfunctions.R")){
    source("~/.RbasicFunctions.R")
    cat(".RbasicFunctions.R was loaded, providing the following functions:\n\n",sep="")
    print.functions()}

The actual file is empty though.

No comments:

Post a Comment