Why is rm(list=ls()) bad practice?
I think it's always difficult and sometimes impossible to take an existing session and convert it to the vanilla state, but it's very easy to run a new instance of R from an existing one. So instead of a clearws() function, I'd suggest a "runInVanilla" function, that takes some code as input, starts up a vanilla session and collects the output. This is quite similar to what reprex::reprex does, maybe not different at all. Duncan Murdoch
On 22/01/2021 10:37 a.m., J C Nash wrote:
Thanks Duncan for a clear argument about the "why".
The suggestion of R --vanilla started a train of thought that one could do something like
clearws <- function(){ # Try to clear workspace
tmp <- readline("Are you sure you want to clear the workspace? ")
print(tmp)
if ( substr(toupper(tmp),1,1) != "Y" ){
return(0)
}
# rm(tmp)
tgt<-parent.env(environment())
print(ls(tgt))
rm(list = ls(tgt, all.names = TRUE),envir=tgt) #will clear all objects includes hidden objects.
gc() #free up memrory and report the memory usage.
# What should we return?
# Can we offer interactive control?
# How about packages?
}
and call clearws() at the start of an example.
Contact me off-list if you have suggestions for improving this. I'd like to be able to preface
examples with such a function that would render the session "vanilla" but from inside. That means
removing packages that might have altered / replaced / masked standard functions. That might not
be possible, but the idea is attractive to me as someone who mostly uses R in tests of tools that
get used by others.
Best, JN
On 2021-01-21 6:05 p.m., Duncan Murdoch wrote:
On 21/01/2021 5:20 p.m., J C Nash wrote:
In a separate thread Jeff Newmiller wrote:
rm(list=ls()) is a bad practice... especially when posting examples. It doesn't clean out everything and it removes objects created by the user.
This query is to ask 1) Why is it bad practice to clear the workspace when presenting an example? I'm assuming here that people who will try R-help examples will not run them in the middle of something else, which I agree would be unfortunates.
I think that's exactly the concern.? I doubt it would have happened in this instance, but in other cases, people might copy and paste a complete example before reading it.? It's safer to say:? "Run this code in a clean workspace:", rather than cleaning it out yourself. Duncan Murdoch However, one of the
not very nice aspects of R is that it is VERY easy to have stuff hanging around (including overloaded functions and operators) that get you into trouble, and indeed make it harder to reproduce those important "minimal reproducible examples".? This includes the .RData contents. (For information, I can understand the attraction, but I seem to have been burned much more often than I've benefited from a pre-warmed oven.) 2) Is there a good command that really does leave a blank workspace? For testing purposes, it would be useful to have an assured blank canvas.
Yes, start R with ? R --vanilla Duncan Murdoch
This post is definitely not to start an argument, but to try to find ways to reduce the possibilities for unanticipated outcomes in examples. Cheers, JN
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.