R: how not to use savehistory() and source()

Admitting to stupidity is part of the learning process. So in the interests of public education, here’s something stupid that I did today.

You’re working in the R console. Happy with your exploratory code, you decide to save it to a file.

savehistory(file = "myCode.R")

Then, you type something else, for example:

# more lines here

And then, decide that you should save again:

savehistory(file = "myCode.R")

You quit the console. Returning to it later, you recall that you saved your code and so can simply run source() to get back to the same point:


Unfortunately, you forget that the sourced file now contains the savehistory() command. Result: since your new history contains only the single line source() command, then that is what gets saved back to the file, replacing all of your lovely code.

Possible solutions include:

  • Remember to edit the saved file, removing or commenting out any savehistory() lines
  • Generate a file name for savehistory() based on a timestamp so as not to overwrite each time
  • Suggested by Scott: include a prompt in the code before savehistory()

At a complete (Rails) loss

I’ve been banging my head against the wall for almost a week with a Rails application. This post is not a plea for help – I’d use a forum for that – just a record of the problem. That said, feel free to comment, especially if you have a similar problem.

This is all using Rails 2.3.2, Mongrel 1.1.5, installed as gems on Ubuntu 9.04.

The basic issue: 2 models, 2 controllers, 2 sets of views. Identical in almost every respect, little more than basic CRUD (index, create, update, destroy). (1) works, (2) does not.

Update – thanks for your comments, here and elsewhere. In the end I rebuilt from scratch with scaffolding and it’s all good so far. Guess there was something rogue in my hand-crafted code.
Read the rest…