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.char...@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@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@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.

Reply via email to