4

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!

oguz ismail
  • 1
  • 16
  • 47
  • 69
Ben Thomas
  • 43
  • 1
  • 4
  • 4
    It's not very clear what you're trying to do. `rm(list=ls())` removes all the *objects* in the global environment. What do you mean by *the code is still there*? Please show an example if you can – Rich Scriven Jan 20 '15 at 00:45
  • 2
    You're typing `ls()` to list the objects, and not `ls` (without `()`), is that correct? – Matthew Lundberg Jan 20 '15 at 00:49
  • when you talk about code, are you talking about the code commands you've run in the history? – Alex Jan 20 '15 at 00:57
  • @MatthewLundberg, I'm typing `ls` _without_ the parentheses. Typing `ls()` with parentheses returns `character(0)` – Ben Thomas Jan 20 '15 at 01:02
  • Perhaps you have replace the standard R `ls`-function with something else. Now type `rm(ls)`. – IRTFM Jan 20 '15 at 05:03

1 Answers1

11

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.

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
  • 6
    Wow. I feel _fantastically_ stupid. If there's no way to delete this thread, I guess I'll live with this shame forever. – Ben Thomas Jan 20 '15 at 01:43
  • 2
    @BenThomas - It's not a bad question at all. I bet quite a few people get confused by that. Hopefully the question will be useful for future SO'ers – Rich Scriven Jan 20 '15 at 01:47
  • @Ben Thomas- very useful question. I'm starting with R ( with 30 years of Matlab experience ) and I had the same query and found my answer here. Good question and good answer. – darbehdar Jun 16 '20 at 07:19