[R] add data to a file while doing a loop

William Dunlap wdunlap at tibco.com
Fri Jan 6 16:43:40 CET 2012


Is this an example of what you want to do?

  fileName <- tempfile(fileext=".txt")
  fileCon <- file(fileName, "wt") # a file connection, opened for writing text
  for(i in 1:5) {
      cat(file=fileCon, "Line", i, "\n") # write to connection, not file name
  }
  close(fileCon)

Then look to see what the file contains:

  > readLines(fileName)
  [1] "Line 1 " "Line 2 " "Line 3 " "Line 4 " "Line 5 "

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Joao Fadista
> Sent: Friday, January 06, 2012 5:27 AM
> To: sas0025 at auburn.edu
> Cc: r-help at r-project.org
> Subject: Re: [R] add data to a file while doing a loop
> 
> Thanks for the reply. Every iteration produces a text data.frame directly into a file without saving
> it in R. And every iteration overwrites the data produced from the previous iteration. So what I would
> like to do is being able not to overwrite the data of any iteration but adding it to the same file
> with maybe the functions paste or rbind.
> 
> 
> > sessionInfo()
> R version 2.13.2 (2011-09-30)
> Platform: x86_64-pc-mingw32/x64 (64-bit)
> 
> locale:
> [1] LC_COLLATE=Swedish_Sweden.1252  LC_CTYPE=Swedish_Sweden.1252    LC_MONETARY=Swedish_Sweden.1252
> LC_NUMERIC=C                    LC_TIME=Swedish_Sweden.1252
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> other attached packages:
> [1] MatrixEQTL_1.2.0
> 
> loaded via a namespace (and not attached):
> [1] tools_2.13.2
> 
> 
> Best regards,
> João Fadista, Ph.D.
> Post Doc
> 
> Lund University Diabetes Centre
> CRC, Malmö University Hospital
> Entrance 72, building 60, level 13
> SE-205 02 Malmö, Sweden
> Tel: +46 (0)40 391237
> e-mail: joao.fadista at med.lu.se
> 
> 
> -----Original Message-----
> From: stephen sefick [mailto:ssefick at gmail.com] On Behalf Of Stephen Sefick
> Sent: den 6 januari 2012 14:14
> To: Joao Fadista
> Cc: r-help at r-project.org
> Subject: Re: [R] add data to a file while doing a loop
> 
> Without context, read reproducible code, it is hard to answer this question.  What system are you on?
> Do you need to write one line of data or a data frame or a list out?  Are trying to write a graphic
> out?
>  It will be easier to answer your question with some context.  Good luck!
> 
> Stephen
> 
> On Fri 06 Jan 2012 05:49:01 AM CST, Joao Fadista wrote:
> > Hi,
> >
> > I would like to know how can I keep adding data to a file while doing a loop and without deleting
> the data of the previous iteration. Thanks.
> >
> > ______________________________________________
> > 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.
> ______________________________________________
> 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.


More information about the R-help mailing list