问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/28035992/rm-doesnt-seem-to-empty-my-r-workspace