R: how not to use savehistory() and source()
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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:
ls() # 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:
source("myCode.R")
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()
Filed under: programming, R, research diary, statistics Tagged: stupidity
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.