objects that contain specified string within their name
I just find it easier than writing every object in rm()
leave<-function(exclude_strings){# Get all objects in the global environmentall_objects<-ls(envir=.GlobalEnv)# Identify functions and objects containing specified strings in their namesfunctions<-all_objects[sapply(all_objects,function(x)is.function(get(x)))]exclude_objects<-all_objects[sapply(all_objects,function(x)any(sapply(exclude_strings,function(s)grepl(s,x))))]# Remove all objects except functions and objects containing specified strings in their namesobjects_to_remove<-setdiff(all_objects,c(functions,exclude_objects))rm(list=objects_to_remove,envir=.GlobalEnv)}
plotsave()
save all objects that are named as str+i (e.g. plot1, plot_1 or p1) in path (path specified folder)