[R] assign() problem

Koen Hufkens koen.hufkens at telenet.be
Wed Nov 23 13:37:04 CET 2005


I've written a piece of code (see below) to do a wavelet image 
decomposition, during  the evaluation of this code I would like to write 
the results of some calculations back to the R root directory. I used 
assign() to do so because the names should vary when going thrue a 
while() loop. For some unknown reason I get an error that says:

Error in assign(varname[i], imwrImage) :
         invalid first argument

what could be wrong, when I do it on the commandline everything works 
out just fine. But within the function it doesn't. When I disable the 
assign statement, everything runs fine so the rest of the code should be 
clean.

My code:

# Wavelet multiresolution decomposition

wmra <- function(image_file){
#require(rimage)
require(wavethresh)

#reading image file and converting it to grayscale
#dimage <- read.jpeg(image_file)
#dimage <- imagematrix(dimage)

# discrete wavelet decomposition
imwdImage <- imwd(image_file)

# get a value for the number of decomposition levels
#nlevels <- imwdImage$nlevels

i <- 0
varname <- paste("level",0:(imwdImage$nlevels-1),sep="")
print(varname)
while ( i < imwdImage$nlevels)
	 {
	
	 # set the threshold to 0 on all levels except the one in evaluation
	 # thresholdLevels is the list of levels to set to zero
	 thresholdLevels <- 1:(imwdImage$nlevels-1)
	 thresholdLevels[i] <- 0
	 thresholdedCoeff <- threshold.imwd(imwdImage,levels=thresholdLevels, 
policy=c("manual"), type="hard", value=10000)
	
          # calculate the inverse wavelet transform
	 imwrImage <- imwr(thresholdedCoeff)

	 # assign the various decomposition level data a name
	 # starting at i+1 because i = 0.
          assign(varname[i+1],imwrImage) #  --> gives an error
	    	
	 # export the multiresolution decomposition for level i as graph
	 jpeg(paste("level",i,".jpg",sep=""),quality=100)	
	 image(imwrImage,xlab=c("MRA image of level: ",i),col=gray(255:0/256))
	 dev.off()
    	 	 	
	 i <- i + 1
	  }

}

Best regards, Koen




More information about the R-help mailing list