Quantcast

R equivalent of Python str()?

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

R equivalent of Python str()?

Ernest Adrogué i Calveras
Hi,

I was wondering if there's a function in R that is meant to return a
string representation of an object. Basically, it's like print() but
it doesn't print anything, it only returns a string.

I know there's a str() function but it's not quite the same. I mean a
function that returns the same string that print() would display.

--
Bye,
Ernest

______________________________________________
[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: R equivalent of Python str()?

jholtman
?dump
?dput

2012/2/7 Ernest Adrogué <[hidden email]>:

> Hi,
>
> I was wondering if there's a function in R that is meant to return a
> string representation of an object. Basically, it's like print() but
> it doesn't print anything, it only returns a string.
>
> I know there's a str() function but it's not quite the same. I mean a
> function that returns the same string that print() would display.
>
> --
> Bye,
> Ernest
>
> ______________________________________________
> [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.



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.

______________________________________________
[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: R equivalent of Python str()?

Michael Weylandt
Possibly as.character() is what the OP was seeking

Michael

On Feb 7, 2012, at 7:15 PM, jim holtman <[hidden email]> wrote:

> ?dump
> ?dput
>
> 2012/2/7 Ernest Adrogué <[hidden email]>:
>> Hi,
>>
>> I was wondering if there's a function in R that is meant to return a
>> string representation of an object. Basically, it's like print() but
>> it doesn't print anything, it only returns a string.
>>
>> I know there's a str() function but it's not quite the same. I mean a
>> function that returns the same string that print() would display.
>>
>> --
>> Bye,
>> Ernest
>>
>> ______________________________________________
>> [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.
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> ______________________________________________
> [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: R equivalent of Python str()?

William Dunlap
In reply to this post by Ernest Adrogué i Calveras
Use
   capture.output(print(yourData))
to capture would be printed by print as a vector
of a strings (one per line of printout).  Paste
together if desired.

Use deparse(yourData) to get a string representation
that can be parsed by R (useful for sending to others
who are using R).

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf Of Ernest Adrogué
> Sent: Tuesday, February 07, 2012 4:09 PM
> To: [hidden email]
> Subject: [R] R equivalent of Python str()?
>
> Hi,
>
> I was wondering if there's a function in R that is meant to return a
> string representation of an object. Basically, it's like print() but
> it doesn't print anything, it only returns a string.
>
> I know there's a str() function but it's not quite the same. I mean a
> function that returns the same string that print() would display.
>
> --
> Bye,
> Ernest
>
> ______________________________________________
> [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: R equivalent of Python str()?

Martin Maechler
In reply to this post by Michael Weylandt
>>>>> "R Michael Weylandt <[hidden email]>"
>>>>> <[hidden email]>
>>>>>     on Tue, 7 Feb 2012 20:23:57 -0500 writes:

    > Possibly as.character() is what the OP was seeking
    > Michael

or   format()   which is closer for numeric data


    > On Feb 7, 2012, at 7:15 PM, jim holtman <[hidden email]> wrote:

    >> ?dump
    >> ?dput
    >>
    >> 2012/2/7 Ernest Adrogué <[hidden email]>:
    >>> Hi,
    >>>
    >>> I was wondering if there's a function in R that is meant to return a
    >>> string representation of an object. Basically, it's like print() but
    >>> it doesn't print anything, it only returns a string.
    >>>
    >>> I know there's a str() function but it's not quite the same. I mean a
    >>> function that returns the same string that print() would display.
    >>>
    >>> --
    >>> Bye, Ernest
    >>>
    >>> ______________________________________________ [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.
    >>
    >>
    >>
    >> --
    >> Jim Holtman Data Munger Guru
    >>
    >> What is the problem that you are trying to solve?  Tell me what you
    >> want to do, not how you want to do it.
    >>
    >> ______________________________________________ [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.

______________________________________________
[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: R equivalent of Python str()?

Ernest Adrogué i Calveras
 8-02-2012, 09:45 (+0100); Martin Maechler escriu:
> >>>>> "R Michael Weylandt <[hidden email]>"
> >>>>> <[hidden email]>
> >>>>>     on Tue, 7 Feb 2012 20:23:57 -0500 writes:
>
>     > Possibly as.character() is what the OP was seeking
>     > Michael
>
> or   format()   which is closer for numeric data
>

Thanks for the suggestions, but none of these appears to do what I
need. Take a table such as

> a <- as.table(c(A=450,B=12))
> a
  A   B
450  12

The string that print() prints is " A B \n450 12 \n". Once you have
the string you can print it with cat() and get the same result as with
print().

> cat("  A   B \n450  12 \n")
  A   B
450  12

The function that I was looking for was one that given the table a in
the example would return the string described above. Apparently,
capture.output() can be used for that, although it returns the string
split into lines they can be joined together easily. So, case closed
:)

--
Bye,
Ernest

______________________________________________
[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: R equivalent of Python str()?

Ernest Adrogué i Calveras
In reply to this post by William Dunlap
 8-02-2012, 04:22 (+0000); William Dunlap escriu:
> Use
>    capture.output(print(yourData))
> to capture would be printed by print as a vector
> of a strings (one per line of printout).  Paste
> together if desired.

This will do it!!

Thanks.

--
Cheers,
Ernest

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