Re: [R] write.table: last line should differ from usual eol

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat, 09 Jun 2007 08:19:16 -0400

Try this:

write.ilog <- function(X, file = "") {

   w <- function(x, z, file)

          cat("[", paste(x, collapse = ","), "]", z, sep = "", file = file)    if (!identical(file, "")) {

      file <- open(file, "w")
      on.exit(close(file))

   }
   cat("X=[", file = file)
   nr <- nrow(X)
   for(i in 1:nr) w(X[i,], if (i == nr) "];\n" else ",\n", file)    invisible(X)
}

X<-array(1:4,c(2,2))
write.ilog(X)

On 6/9/07, jim holtman <jholtman_at_gmail.com> wrote:
> This will probably do it for you. It is a function to create the output:
>
>
>
> write.array <- function(x,fileName){
> outFile <- file(fileName, 'w')
> cat(deparse(substitute(x)), "=[", sep='', file=outFile)
> for (i in 1:nrow(x)){
> cat('[', paste(x[i,], collapse=','), ']', file=outFile, sep='')
> if (i == nrow(x)) cat('];', file=outFile, sep='')
> else cat(',\n', file=outFile, sep='')
> }
> close(outFile)
> }
>
> # test data
> a <- matrix(1:25,5)
> write.array(a, '/tempxx.txt')
>
> Here is the output file:
>
> a=[[1,6,11,16,21],
> [2,7,12,17,22],
> [3,8,13,18,23],
> [4,9,14,19,24],
> [5,10,15,20,25]];
>
>
>
>
> I have a problem with writing an array to (for example) a .txt-file.
> Because of the .txt-file must be read from another programm (OPL ILOG),
> the syntax of the output must be from a special form:
>
> name_of_the_object = [ [1,2, ... ],
> [1,...],
> ... ];
>
> I think it's easier to understand with a small example:
>
> X<-array(1:4,c(2,2))
>
> should be written as:
> X = [[1,3],
> [2,4]];
>
>
> I have (until now) used the following:
>
> write("X=[[",file=filename)
> write.table(X,file=filename,sep=",",eol="],\n [", row.names=FALSE,
> col.names=FALSE,append=TRUE)
>
> which leads to the following output:
> X=[[
> 1,3],
> [2,4],
> [
>
> I hope you can help because it's very annoying to adjust the resulting
> .txt-file "by hand".
>
> Thanks a lot for your help!
> With nice greetings
>
> Andreas Gegg,
> mathematic-student on Catholic University of Eichstätt-Ingolstadt (Germany)
>
> ______________________________________________
> 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
> 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?
>
> [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
>



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 and provide commented, minimal, self-contained, reproducible code. Received on Sat 09 Jun 2007 - 12:27:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 09 Jun 2007 - 14:31:28 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.