Skip to content

Show only header of str() function

9 messages · Enrico Schumann, Avi Gross, Deepayan Sarkar +2 more

#
Hello, is it possible to show only the header (that is: `'data.frame':
x obs. of  y variables:` part) of the str function?
Thank you
#
Hello,

Not perfect but works for data.frames:


header_str <- function(x){
   capture.output(str(x))[[1]]
}
header_str(iris)
header_str(AirPassengers)
header_str(1:10)


Hope this helps,

Rui Barradas

?s 12:02 de 02/09/21, Luigi Marongiu escreveu:
#
Thank you! better than dim() anyway.
Best regards
Luigi
On Thu, Sep 2, 2021 at 1:31 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:

  
    
#
Luigi,

If you are sure you are looking at something like a data.frame, and all you
want o know is how many rows and how many columns are in it, then str() is
perhaps too detailed a tool.

The functions nrow() and ncol() tell you what you want and you can get both
together with dim(). You can, of course, print out whatever message you want
using the numbers supplied by throwing together some function like this:

sstr <- function(x) {
  cat(nrow(x), "obs. of ", ncol(x), " variables\n")
}

Calling that instead of str may meet your needs.  Of course, unlike str, it
will not work on arbitrary data structures.

Note the output of str()goes straight to the screen, similar to what cat
does. Capturing the output to say chop out just the first line is not
therefore a simple option. 


-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Luigi Marongiu
Sent: Thursday, September 2, 2021 7:02 AM
To: r-help <r-help at r-project.org>
Subject: [R] Show only header of str() function

Hello, is it possible to show only the header (that is: `'data.frame':
x obs. of  y variables:` part) of the str function?
Thank you

--
Best regards,
Luigi

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.
#
On Thu, 02 Sep 2021, Luigi Marongiu writes:
Perhaps one more solution. You could limit the number
of list components to be printed, though it will leave
a "truncated" message.

    str(iris, list.len = 0)
    ## 'data.frame':    150 obs. of  5 variables:
    ##   [list output truncated]

Since 'str' is a generic function, you could also
define a new 'str' method. Perhaps something among
those lines:

    str.data.frame.oneline <- function (object, ...) {
        cat("'data.frame':\t", nrow(object), " obs. of  ",
            (p <- length(object)), 
            " variable", if (p != 1) "s", "\n", sep = "")
        invisible(NULL)
    }

(which is essentially taken from 'str.data.frame').

Then:

    class(iris) <- c("data.frame.oneline", class(iris))

    str(iris)
    ## 'data.frame':  150 obs. of  5 variables
    
    str(list(a = 1,
             list(b = 2,
                  c = iris)))
    ## List of 2
    ##  $ a: num 1
    ##  $  :List of 2
    ##   ..$ b: num 2
    ##   ..$ c:'data.frame':   150 obs. of  5 variables
#
Thanks for the interesting method Rui. So that is a way to do a redirect of output not to a sinkfile but to an in-memory variable as a textConnection.

Of course, one has to wonder why the makers of str thought it would be too inefficient to have an option that returns the output in a form that can be captured directly, not just to the screen. 

I have in the past done odd things such as using sink() to capture the output of a program that wrote another program dynamically in a loop. The saved file could then be used with source(). So a similar technique can capture the output from str() or cat() or whatever normally only writes to the screen and then the file can be read in to get the first line or whatever you need. I have had to play games to get the right output from some statistical programs too as it was assumed the user would read it, and sometimes had to cherry pick what I needed directly from withing the underlying object.

I suspect one reason R has so many packages including the tidyverse I like to use, is because the original R was designed in another time and in many places is not very consistent. I wonder how hard it would be to change some programs to simply accept an additional argument like sink() has where you can say split=TRUE and get a copy of what is being diverted to also come to the screen. I find cat() to be a very useful way to put more complicated output together than say print() but since it does not allow capture of the text into variables, I end up having to use other methods such as the glue() function or something like print(sprint("Hello %s, I have %d left.\n", "Brian", 5))

But you work with what you have. Your solution works albeit having read the function definition, is quite a bit of overkill when I read the code as it does things not needed. But as noted, if efficiency matters and you are only looking at data.frame style objects, there are cheaper solutions.


-----Original Message-----
From: R-help <r-help-bounces at r-project.org> On Behalf Of Rui Barradas
Sent: Thursday, September 2, 2021 7:31 AM
To: Luigi Marongiu <marongiu.luigi at gmail.com>; r-help <r-help at r-project.org>
Subject: Re: [R] Show only header of str() function

Hello,

Not perfect but works for data.frames:


header_str <- function(x){
   capture.output(str(x))[[1]]
}
header_str(iris)
header_str(AirPassengers)
header_str(1:10)


Hope this helps,

Rui Barradas

?s 12:02 de 02/09/21, Luigi Marongiu escreveu:
______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
#
On Thu, Sep 2, 2021 at 9:26 PM Enrico Schumann <es at enricoschumann.net> wrote:
Or use 'max.level', which is also generally useful for nested lists:

str(iris, max.level=0)
## 'data.frame':    150 obs. of  5 variables:

Best,
-Deepayan
#
Hello,

I believe but do not have references that str was meant for interactive 
use, not for use in a script or package. If this is the case, then it 
should be rare to have to output to an object such as a character vector.

As for my solution, it is far from perfect, I try to avoid 
capture.output and, once again, limit its use to interactive R. It is 
overkill but I use it so few times that performance issues probably do 
not matter. It is sometimes a convenient way of solving an immediate 
problem and once done, move on.

As for the OP, Enrico's solution seems better, even with the 2nd printed 
line. Unless the 1st line is to be processed (?).

Hope this helps,

Rui Barradas

?s 17:32 de 02/09/21, Avi Gross via R-help escreveu:
#
Thanks, that is perfect!

On Thu, Sep 2, 2021 at 7:02 PM Deepayan Sarkar
<deepayan.sarkar at gmail.com> wrote: