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!
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With