[R] readLines problem in iteration

jim holtman jholtman at gmail.com
Mon Jun 23 01:05:48 CEST 2008


What is the value of 'line' on the first iteration?  It appears to be
undefined.  You probably want something like this:

while (TRUE){
    line <- readLines(con, n=1)
    if (length(line) == 0) break
    .....your normal statements.....
}

On Sun, Jun 22, 2008 at 3:29 PM, Yasin Hajizadeh
<yasin_reservoir at yahoo.com> wrote:
> Hi there
>  My script at each iteration produces a .dat file which I save in a directory with write.table. Then I need to read each line of this file and create a directory for each line and put elements of that row in that directory. I do it with the following script which I have inserted somewhere between my outer iteration loop, it works fine for the first iteration, but the problem is that it does nothing at next iterations. How can I solve this problem, is there anything wrong with my code?
>  Thanks in advance for your help.
>
>
>    write.table(X, file = "model.dat", append = FALSE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = FALSE, col.names = FALSE, qmethod = c("escape", "double"))
>
>  mother<-getwd()
>
>
>  count <- 0;
>
>  con<- file("model.dat", open="r")
>
>
>  while (length(line) > 0) {
>
>  line<-readLines(con,n=1)
>  count <- count +1
>  cat("read row no", count, ",\n", sep="");
>  print(line)
>
>  foldername <- paste("Iteration",c, "ant", count)
>  dir.create (foldername)
>
>  setwd(foldername)
>  write.table(line, file = "params.in", append = FALSE, sep = " ", eol = "\n", na = "NA", dec = ".", row.names = FALSE, col.names = FALSE, qmethod = c("escape", "double"))
>
>  #system(run_misf_calc.py)
>
>  setwd(mother)
>
>
>  }
>
>
>  close(con)
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list