[R] assign() problem

bogdan romocea br44114 at gmail.com
Wed Nov 23 15:52:17 CET 2005


Don't use assign(), named lists are much better (check the stuff on
indexing lists). Here's an example:
a <- list()
a[["one"]] <- c(1,2,3)
a[["two"]] <- c(4,5,6)
a[["two"]]
do.call("rbind",a)
do.call("cbind",a)
lapply(a,sum)

With regards to your question, did you try printing varname[i] in your
loop to see which value causes the error message?


> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Koen Hufkens
> Sent: Wednesday, November 23, 2005 7:37 AM
> To: R help mailinglist
> Subject: [R] assign() problem
>
>
> 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
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list