R allows us to put code to run in the beginning/end of a session.

What codes would you suggest putting there?

I know of three interesting examples (although I don't have "how to do them" under my fingers here):

  1. Saving the session history when closing R.
  2. Running a fortune() at the beginning of an R session.
  3. I was thinking of having an automated saving of the workspace. But I didn't set on solving the issue of managing space (so there would always be X amount of space used for that backup)

Any more ideas? (or how you implement the above ideas)

p.s: I am not sure if to put this here or on stackoverflow. But I feel the people here are the right ones to ask.

  • $\begingroup$ Probably better suited to stackoverflow since it has no particular data analysis relevance. $\endgroup$
    – Shane
    Aug 3, 2010 at 8:28
  • $\begingroup$ This question is proposed to be closed. See this meta thread: meta.stats.stackexchange.com/questions/213/… $\endgroup$
    – user28
    Aug 3, 2010 at 8:58
  • 1
    $\begingroup$ I wish we could move a question to Stack Overflow the way you can move a question from Super User... $\endgroup$
    – JD Long
    Aug 3, 2010 at 20:42
  • $\begingroup$ I moved it manually to stackoverflow. JD - I agree with you. $\endgroup$
    – Tal Galili
    Aug 4, 2010 at 20:14
  • $\begingroup$ Following on from meta.stats.stackexchange.com/questions/252/… I've reopened this question. $\endgroup$ Aug 11, 2010 at 10:43

2 Answers 2


Some information about how to implement this is provided at help(.First) and help(.Last).


On open, I set R options, load environment variables (eg. global variables, API keys) and open database connections, and then close those connections when exiting. With some of these things, I prefer to do them onLoad of my packages rather than per session.

Regarding how to save your session, use the save command.


Not the answer you're looking for? Browse other questions tagged or ask your own question.