Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does r clear all global environment quickly [duplicate]

Tags:

r

I'm trying to clear my R workspace. Nothing I've found in any thread seems to work - and I've been googling and trying solutions for hours now :(

When I open R and type ls, the console displays all the code from a previous session:

function (name, pos = -1L, envir = as.environment(pos), all.names = FALSE, 
    pattern) 
{
    if (!missing(name)) {
        nameValue <- try(name, silent = TRUE)
        if (identical(class(nameValue), "try-error")) {
            name <- substitute(name)
            if (!is.character(name)) 
                name <- deparse(name)
            warning(gettextf("%s converted to character string", 
                sQuote(name)), domain = NA)
            pos <- name
        }
        else pos <- nameValue
    }
    all.names <- .Internal(ls(envir, all.names))
    if (!missing(pattern)) {
        if ((ll <- length(grep("[", pattern, fixed = TRUE))) && 
            ll != length(grep("]", pattern, fixed = TRUE))) {
            if (pattern == "[") {
                pattern <- "\\["
                warning("replaced regular expression pattern '[' by  '\\\\['")
            }
            else if (length(grep("[^\\\\]\\[<-", pattern))) {
                pattern <- sub("\\[<-", "\\\\\\[<-", pattern)
                warning("replaced '[<-' by '\\\\[<-' in regular expression pattern")
            }
        }
        grep(pattern, all.names, value = TRUE)
    }
    else all.names
}
<bytecode: 0x2974f38>
<environment: namespace:base>

If I type rm(list=ls()) and then type ls again, I get the exact same response - i.e., the code from the previous session hasn't been removed.

By the way, I'm typing ls without the parentheses. Typing ls() with parentheses returns character(0).

I've also tried clearing the environment via RStudio, and even deleting the ~/.Rdata file. Nothing will clear this workspace. Every time I restart R and type ls, all the old code is still there.

I've already tried the tips in this thread, and they don't work for me.

Any idea why this might be happening? Thanks!

like image 678
Ben Thomas Avatar asked Jan 20 '15 00:01

Ben Thomas


People also ask

How do I permanently clear global environment in R?

You can do both by restarting your R session in RStudio with the keyboard shortcut Ctrl+Shift+F10 which will totally clear your global environment of both objects and loaded packages.

Which command deletes all of the objects from the global environment?

The rm() code removes objects in your workspace. You can begin your code with the rm() function to clear all of the objects from your workspace to start with a clean environment. This way the workspace is empty and everything you create is clearly visible.

How do I clear all codes in R?

The shortest and the quickest way to clear the global environment in R is by using shortcut keys from the keyboards. Simply hit Ctrl+L on the keyboard and you will see that everything written in the console will be erased and the console will be cleared.

How do I remove multiple objects from the environment in R?

rm() function in R Language is used to delete objects from the memory. It can be used with ls() function to delete all objects. remove() function is also similar to rm() function.


1 Answers

What you are seeing is the source code for the ls function. When you enter a function name without the parentheses, you'll see the complete source code for that function (provided that function is in one of the packages attached to the search path, or in the global environment).

When you see character(0) as the result of calling ls(), that means that there are no objects in the global environment. The base package, where ls calls home, is different from the global environment, and objects there cannot be removed.

When character(0) is the result of ls() after you call rm(list=ls()), you have successfully cleared the objects in the global environment.

like image 147
Rich Scriven Avatar answered Sep 20 '22 11:09

Rich Scriven