Remove All Variables Except Functions

remove all variables except functions

Here's a one-liner that removes all objects except for functions:

rm(list = setdiff(ls(), lsf.str()))

It uses setdiff to find the subset of objects in the global environment (as returned by ls()) that don't have mode function (as returned by lsf.str())

How can I remove all objects but one from the workspace in R?

Here is a simple construct that will do it, by using setdiff:

rm(list=setdiff(ls(), "x"))

And a full example. Run this at your own risk - it will remove all variables except x:

x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"

rm(list=setdiff(ls(), "x"))

ls()
[1] "x"

How can I remove all objects but one from the workspace in R?

Here is a simple construct that will do it, by using setdiff:

rm(list=setdiff(ls(), "x"))

And a full example. Run this at your own risk - it will remove all variables except x:

x <- 1
y <- 2
z <- 3
ls()
[1] "x" "y" "z"

rm(list=setdiff(ls(), "x"))

ls()
[1] "x"

rm( ) everything except specific object

The setdiff() function shows the difference between sets, so we can use this to give the difference between all the objects (ls()), and the object you want to keep. For example

## create some objects
df <- data.frame()
v <- as.numeric()

# show everything in environment
objects()
# [1] "df" "v"

## or similarly
ls()
# [1] "df" "v"

## the setdiff() funciton shows the difference between two sets
setdiff(ls(), "df")
# [1] "v"

# so we can use this to remove everything except 'df'
rm(list = setdiff(ls(), "df"))
objects()
# [1] "df"

Function to remove all variables

You need to pass the parent.frame() environment to ls, not just to rm. Otherwise ls won't find the variables to remove.

clVar <- function()
{
env <- parent.frame()
rm(
list = setdiff( ls(all.names=TRUE, env = env), lsf.str(all.names=TRUE, env = env)),
envir = env
)
}

How to remove all user created variables that start with a specific set of strings

You can probably do this using a regular expression via ls:

rm(list = ls(pattern = "^tmp"))

dplyr: select all variables except for those contained in vector

select(df, -any_of(excluded_vars))
is now the safest way to do this (the code will not break if a variable name that doesn't exist in df is included in excluded_vars)

Is there a way to delete created variables, functions, etc from the memory of the interpreter?

You can delete individual names with del:

del x

or you can remove them from the globals() object:

for name in dir():
if not name.startswith('_'):
del globals()[name]

This is just an example loop; it defensively only deletes names that do not start with an underscore, making a (not unreasoned) assumption that you only used names without an underscore at the start in your interpreter. You could use a hard-coded list of names to keep instead (whitelisting) if you really wanted to be thorough. There is no built-in function to do the clearing for you, other than just exit and restart the interpreter.

Modules you've imported (import os) are going to remain imported because they are referenced by sys.modules; subsequent imports will reuse the already imported module object. You just won't have a reference to them in your current global namespace.

Python doesn’t make any security guarantees about data in memory however. When objects no longer are referenced the interpreter marks the memory as no longer in use but does not take steps to overwrite that memory to prevent access to data. If you need that level of security protection you’ll need to use third-party extensions that manage their own memory with security in mind.



Related Topics



Leave a reply



Submit