Writing output into a file

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing output into a file

surangak
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

Alfredo Alessandrini
Hi,

could you paste the results?


Alfredo

2012/2/12 Suranga Kasthurirathne <[hidden email]>:

> Hi everyone,
>
> I'm an R newbie working with the poLCA module. I achieved my target without
> having to bother anyone, but It seems that I've got stuck at the last
> minute.
>
> My problem is simple. I need to write my results into a file.
> My results are in the shape of a list (unbalanced columns)
> I've considered several methods (sink(), write.file) etc. etc.
> Unfortunately, I'm not the best brains in the market on this subject.
> I've also faced some difficulty in converting the list so that it can be
> written using write.file().
>
> Therefore, I'm wondering if anyone can point me towards a good example that
> shows me how to write a list into a file safely.
>
>
> --
> Thanks and Best Regards,
>
> Suranga
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

Rui Barradas
In reply to this post by surangak
Hello

One way is

# Write the file
save(myList, file="test1.bin")

# Reload the data, under the same name, 'myList'
load(file="test1.bin")

Another way is a bit more complicated

# Open a file connection and write the list to it (using  comma as separator)
fileCon <- file("test2.txt", open="wt")
lapply(myList, function(x) writeLines(paste(x, collapse=","), con=fileCon))
close(fileCon)

# Load the data, maybe under another name
strsplit(readLines(con="test2.txt"), split=",")

If you use the first method, the list is retrieved as it was.
If you use the second, you lose the list's members' names.

Hope this helps,

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

surangak
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

surangak
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

Rui Barradas
Hello,

> I tried writing this data into a file using the save(myList,
> file="test1.bin") command, but unfortunately, the numerical values seem
> to get garbled when I do so.
>
> The numbers in my RGui look like
>
> 0, 0.5, 0, 1 etc. etc.
>
> But when I stored it into a .bin file, and retrieved it using java code,
> it returns data such as,
>

The problem should be in the use of java, 'save' uses a R format , RDA.
You can use 'ascii=TRUE'and see it with a text editor. Also see

?save

> I also tried the second method (using a # Open a file connection)
>
> Unfortunately, here too the data gets extremely garbled.

Don't understand why, check the output file with a text editor and let us know
what is wrong.

The problem I've seen is that the use of 'strsplit' coerses the numeric data to character,
but this is easy to solve.

Does your list have sub-lists?

Rui Barradas
Reply | Threaded
Open this post in threaded view
|

Re: Writing output into a file

surangak
CONTENTS DELETED
The author has deleted this message.