Quantcast

print.data.frame to string?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

print.data.frame to string?

ivo welch
dear R experts---is there a function that prints a data frame to a string?
 cat() cannot handle lists, so I cannot write cat("your data frame is:\n",
df, "\n").

regards, /iaw
----
Ivo Welch ([hidden email])

        [[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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: print.data.frame to string?

glsnow
What do you mean by prints?  You can use capture.output to get what
would regularly be printed to the screen into a text vector, or use
dput to get a version of an object that could be read back into
another R session.

On Thu, May 31, 2012 at 2:10 PM, ivo welch <[hidden email]> wrote:

> dear R experts---is there a function that prints a data frame to a string?
>  cat() cannot handle lists, so I cannot write cat("your data frame is:\n",
> df, "\n").
>
> regards, /iaw
> ----
> Ivo Welch ([hidden email])
>
>        [[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.



--
Gregory (Greg) L. Snow Ph.D.
[hidden email]

______________________________________________
[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
|  
Report Content as Inappropriate
star

Re: print.data.frame to string?

Jeff Newmiller
In reply to this post by ivo welch
capture.output(print(mydf))

note that df is a base function... best to not use it as a variable.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

ivo welch <[hidden email]> wrote:

>dear R experts---is there a function that prints a data frame to a
>string?
>cat() cannot handle lists, so I cannot write cat("your data frame
>is:\n",
>df, "\n").
>
>regards, /iaw
>----
>Ivo Welch ([hidden email])
>
> [[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
|  
Report Content as Inappropriate
star

Re: print.data.frame to string?

ivo welch
thanks, jeff.  no, not capture.output(), but thanks for pointing me to it
(I did not know it).  capture.output flattens the data frame.  I want the
print.data.frame output, so that I can feed it to cat, and get reasonable
newlines, too.

regards,

/iaw
----
Ivo Welch ([hidden email])
J. Fred Weston Professor of Finance
Anderson School at UCLA, C519
http://www.ivo-welch.info/
Editor, Critical Finance Review, http://www.critical-finance-review.org/


On Thu, May 31, 2012 at 1:19 PM, Jeff Newmiller <[hidden email]>wrote:

> capture.output(print(mydf))
>
> note that df is a base function... best to not use it as a variable.
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> ---------------------------------------------------------------------------
> Sent from my phone. Please excuse my brevity.
>
> ivo welch <[hidden email]> wrote:
>
> >dear R experts---is there a function that prints a data frame to a
> >string?
> >cat() cannot handle lists, so I cannot write cat("your data frame
> >is:\n",
> >df, "\n").
> >
> >regards, /iaw
> >----
> >Ivo Welch ([hidden email])
> >
> >       [[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.
>
>

        [[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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: print.data.frame to string?

David Carlson
It will work if you paste a "\n" to the end of each line:

a <- data.frame(x=runif(4), y=runif(4), z=runif(4))
b <- capture.output(a)
c <- paste(b, "\n", sep="")
cat("Your data set is:\n", c, "\n")

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352

> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of ivo welch
> Sent: Thursday, May 31, 2012 3:31 PM
> To: Jeff Newmiller
> Cc: r-help
> Subject: Re: [R] print.data.frame to string?
>
> thanks, jeff.  no, not capture.output(), but thanks for pointing me to
> it
> (I did not know it).  capture.output flattens the data frame.  I want
> the
> print.data.frame output, so that I can feed it to cat, and get
> reasonable
> newlines, too.
>
> regards,
>
> /iaw
> ----
> Ivo Welch ([hidden email])
> J. Fred Weston Professor of Finance
> Anderson School at UCLA, C519
> http://www.ivo-welch.info/
> Editor, Critical Finance Review, http://www.critical-finance-
> review.org/
>
>
> On Thu, May 31, 2012 at 1:19 PM, Jeff Newmiller
> <[hidden email]>wrote:
>
> > capture.output(print(mydf))
> >
> > note that df is a base function... best to not use it as a variable.
> > ---------------------------------------------------------------------
> ------
> > Jeff Newmiller                        The     .....       .....  Go
> Live...
> > DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live
> > Go...
> >                                      Live:   OO#.. Dead: OO#..
> Playing
> > Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> > /Software/Embedded Controllers)               .OO#.       .OO#.
> rocks...1k
> > ---------------------------------------------------------------------
> ------
> > Sent from my phone. Please excuse my brevity.
> >
> > ivo welch <[hidden email]> wrote:
> >
> > >dear R experts---is there a function that prints a data frame to a
> > >string?
> > >cat() cannot handle lists, so I cannot write cat("your data frame
> > >is:\n",
> > >df, "\n").
> > >
> > >regards, /iaw
> > >----
> > >Ivo Welch ([hidden email])
> > >
> > >       [[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.
> >
> >
>
> [[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
|  
Report Content as Inappropriate
star

Re: print.data.frame to string?

ivo welch
great.  thanks.  exactly what I wanted.  /iaw
----
Ivo Welch ([hidden email])

On Thu, May 31, 2012 at 2:53 PM, David L Carlson <[hidden email]> wrote:

> a <- data.frame(x=runif(4), y=runif(4), z=runif(4))
> b <- capture.output(a)
> c <- paste(b, "\n", sep="")
> cat("Your data set is:\n", c, "\n")
>

        [[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.
Loading...