[R] Selectively Removing objects

Carl Witthoft carl at witthoft.com
Mon Feb 2 23:45:00 CET 2009


Slightly related to this (I think Mr. Rydevik's code solved the question),
is a silly thing I wrote up one weekend.  It started out as a clone of 
the unix  "rm -i" command, and kept on going out of control :-)

I'm not claiming this is clean, or the best way to do this, but it does 
let you apply a wide variety of functions to your collection of 'items'

Carl

-------------
askrm<-function(items,fn="rm",ask=TRUE){
	killed<-NA
	thecall<-vector('list')
	j<-1
	
for (thenam in c(items)){
	if(ask==TRUE){
		prmpt<-paste("Do ",fn," on ",thenam,"? ")
  		readline(prompt=prmpt)->theans
  		}
  		else theans="y"
  	if(theans=="y"){
  		
  		#have to get to parent envir. to find the object of interest
  		#as.name() gets rid of quotes...
  		# paste() dumps all output into a single element of list
  		# Note that,e.g., str() returns nothing, just cats to screen.
  		eval(call(fn,as.name(thenam)),envir=parent.frame(1))->evout
  		paste(evout,collapse=" ")->thecall[j]
  		cat("the result is ", as.character(thecall[j]),'\n')
  		killed[j]<-thenam
  		j<-j+1
  		}
  	
  	}
  #keeping track of what happened
  outs<-list(killed=killed, calls=thecall)
  return(invisible(outs))
  }




More information about the R-help mailing list