Cable, Samuel B Civ USAF AFMC AFRL/RVBXI
2010-Jun-30 14:45 UTC
[R] What is wrong with this code?
Here it is: read.inputs<-function(infile) { for (counter in 1:length(input.list)) { seek(infile,where=0,origin='start') newline<-readLines(infile,n=1) while(length(newline)>0) { if(!is.na(grep(as.character(input.list[[counter]][1]),newline)[1])) { break } newline<-readLines(infile,n=1) } if (length(newline)>0) input.list[[counter]][2](infile) } } Conceptually, it is pretty simple: I just want to read through a short file, looking for several key words ('input.list[[counter]][1]'). If one of these words is found, the reading loop is exited and the function appropriate to found word ('input.list[[counter]][2]') is called. The code stops running and returns an error of "attempt to apply non-function" the very first time it hits the "break" statement, which seems to me to be the most innocuous part of the entire routine. Putting in debug statements, it seems that it crashes right at "break". Why on Earth would R not like a "break" statement? Also, there must be a more elegant way to do character matching than the "grep" thing I have kluged together. Any ideas on that are welcome as well. Thank you very much for your help.
Could you please provide a small reproducible example of the input you have and output you'd like, as the posting guide asks for? There most likely is much simplified way to do what you want, avoiding for loops and therefore `break` altogether. Cable, Samuel B Civ USAF AFMC AFRL/RVBXI wrote:> Here it is: > > read.inputs<-function(infile) { > > for (counter in 1:length(input.list)) { > seek(infile,where=0,origin='start') > newline<-readLines(infile,n=1) > while(length(newline)>0) { > > if(!is.na(grep(as.character(input.list[[counter]][1]),newline)[1])) { > break > } > newline<-readLines(infile,n=1) > } > if (length(newline)>0) input.list[[counter]][2](infile) > } > > } > > Conceptually, it is pretty simple: I just want to read through a short > file, looking for several key words ('input.list[[counter]][1]'). If > one of these words is found, the reading loop is exited and the function > appropriate to found word ('input.list[[counter]][2]') is called. > > The code stops running and returns an error of "attempt to apply > non-function" the very first time it hits the "break" statement, which > seems to me to be the most innocuous part of the entire routine. > Putting in debug statements, it seems that it crashes right at "break". > Why on Earth would R not like a "break" statement? > > Also, there must be a more elegant way to do character matching than the > "grep" thing I have kluged together. Any ideas on that are welcome as > well. > > Thank you very much for your help. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.