Skip to content
Back to formatted view

Raw Message

Message-ID: <87y28fynrn.fsf@enricoschumann.net>
Date: 2021-09-02T15:56:28Z
From: Enrico Schumann
Subject: Show only header of str() function
In-Reply-To: <CAMk+s2Q9QxN9fNdCEyEgjXw4Gvn72K4C=3N2r7cm7Q94g7XX2w@mail.gmail.com> (Luigi Marongiu's message of "Thu, 2 Sep 2021 13:02:05 +0200")

On Thu, 02 Sep 2021, Luigi Marongiu writes:

> 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

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




-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net