Well, here's one way you could do it:

# Don't run this unless you really mean it
clear <- function(){rm(list=ls(.GlobalEnv), envir = .GlobalEnv)}

Both calls to .GlobalEnv seem necessary so that both rm() and ls() go
everywhere with it. However, this certainly isn't the most useful code
because it clears itself...

I'm not the best with environments so I'll let someone else work out the
problems with your other attempts, but I believe the problem with the first
is that it only executes inside the function environment and not the global
environment. Not sure about the second...

Michael Weylandt


On Sat, Aug 27, 2011 at 9:25 AM, Tyler Rinker <tyler_rin...@hotmail.com>wrote:

>
> A previous attempt at this question resulted in the message running
> together, making the message difficult to read and the code lines hard to
> distinquinsh. In my R learning I've come across a situation in which a piece
> of code that works on the work space outside a function does not work inside
> the function. WARNING THIS EMAIL CONTAINES THE CODE:#rm(list=ls())     THIS
> WILL CLEAR ALL OBJECTS FROM YOUR WORKSPACE! When I use rm(list=ls()) and
> then ls() it shows character(0) So I tried to make a quick function to speed
> this up as follows:
>
> #====================
> #    ATTEMPT 1
> #====================
> clear <- function()rm(list=ls())clear()
> ls()  #all objects are still attached
> #====================
> #    ATTEMPT 2
> #====================
> clear <- function(){
> {CLEAR <- function()rm(list=ls())}
> eapply(globalenv(),CLEAR)
> }clear()ls()
> #============================
> #ERROR MESSAGE FRPM ATTEMPT 2
> #============================
>  clear()
> Error in FUN(list(function (x)  : unused argument(s) (list(function (x)
>
> QUESTIONS:Why does this code not work inside the function?  Please critique
> both my attempts.
> What would I need to do to make the pieces of code work inside the
> function?
> Windows 7
> R version 2.14 beta
>
> Thanks in advance,
> Tyler Rinker
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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.
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
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.

Reply via email to