[R] Print list to text file with list elements names

Ingrid Charvet Ingrid.Charvet at rms.com
Wed Oct 1 19:41:16 CEST 2014


Hi Bill, thanks for your suggestions.

Regarding the list elements names, your idea works well - thank you.

However, my first issue is not resolved. I omitted the second sink statement, and R still created a text file with duplicate list elements.
Then I omitted both sink statements (keeping only the lapply), but obviously then no text file is created at all (although it does "print" properly on the R console) ...

I also tried:

> func <- function(i) { write.table(inventory[[i]],file=sprintf("test_new%d",i)) }
> lapply(1:10,func)

But here of course I get 10 different text files (one per list element) when what I would like is to have them all in one file...

Ingrid



-----Original Message-----
From: William Dunlap [mailto:wdunlap at tibco.com]
Sent: 01 October 2014 15:54
To: Ingrid Charvet
Cc: r-help at r-project.org
Subject: Re: [R] Print list to text file with list elements names

Omit the sink() statements to see what is happening -
lapply(myList,print) prints
each item in the list and then the output of lapply is printed via the autoprinting mechanism.
If you put this into a function or saved the return value of lapply into a variable or wrapped the call to lapply in a call to invisible() then the autoprinting would not happen.

You said that you wanted the name each list item printed before the item.  print(myList) would do that, but I assume you've already tried that and didn't like the format.  You can get your own formatting of the name with something like
  > myList <- list(First=data.frame(x=1:2,y=letters[1:2]),
Second=data.frame(x=1:3,z=LETTERS[24:26]))
  > invisible(lapply(seq_along(myList), function(i){ cat(sep="", "\n", names(myList)[i], ":\n") ; print(myList[[i]])}))

  First:
    x y
  1 1 a
  2 2 b

  Second:
    x z
  1 1 X
  2 2 Y
  3 3 Z




Bill Dunlap
TIBCO Software
wdunlap tibco.com


On Wed, Oct 1, 2014 at 3:59 AM, Ingrid Charvet <Ingrid.Charvet at rms.com> wrote:
> Hi everyone,
>
> I want to write a list of data frames to a text file and preserve the names given to the list elements (I am using R 3.1.0).
>
> I tried:
>
> setNames(myList, myNames) # myNames is a vector of char elements same
> length as myList
>
> sink(sprintf("%s",filename))
> lapply(myList,print)
> sink()
>
> And here I have two problems:
>
> 1.       R writes each element of my list to the text file twice, so for example if I have a list with 2 elements (i.e. data frames) in it, it will write 4: in order data frame 1, data frame 2, data frame 1, data frame 2.
>
> 2.       The names of list elements do not print to the text file.
>
> Any suggestions to solve these issues would be appreciated!
> Many thanks,
>
> Ingrid
>
>
>
>
> ________________________________
> This message and any attachments contain information that may be RMS Inc. confidential and/or privileged. If you are not the intended recipient (or authorized to receive for the intended recipient), and have received this message in error, any use, disclosure or distribution is strictly prohibited. If you have received this message in error, please notify the sender immediately by replying to the e-mail and permanently deleting the message from your computer and/or storage system.
>
>
>
>         [[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.


This message and any attachments contain information that may be RMS Inc. confidential and/or privileged.  If you are not the intended recipient (or authorized to receive for the intended recipient), and have received this message in error, any use, disclosure or distribution is strictly prohibited.   If you have received this message in error, please notify the sender immediately by replying to the e-mail and permanently deleting the message from your computer and/or storage system.

	[[alternative HTML version deleted]]



More information about the R-help mailing list