Question
Is rm(list=ls()) at the beginning of a R job that's run in batch useful or otherwise a best practice?
Answer
Yes, it can be useful to start off your batch script with the command 'rm(list=ls())' just to make sure that you have a clean R environment before submitting the batch job. For example, you may have users that have their own .Rprofile files with a sequence of commands in them that create some R objects on startup of R. In that case it may be useful, to erase any existing R objects before running the R code for the batch job.
For example, here is pseudocode to do this:
x <- ls()
if (x is not empty) {
rm(list=ls()
}
Yes, you can certainly run code like this after startup to remove existing R objects from the user workspace.