[R] Using str() in a function.

David Winsemius dwinsemius at comcast.net
Sat Jul 9 15:55:22 CEST 2011

On Jul 9, 2011, at 4:20 AM, andrewH wrote:

> Using str() in a function.
> I am in the early phase of learning R, and I find I spend a lot of  
> time
> trying to figure out what is actually in objects I have created or  
> read in
> from a file.  I'm trying to make a simple little function to display a
> couple of things about a object, let's say the summary() and the  
> str(),
> sequentially, preferably without a bunch of surplus lines between  
> them.  I
> have tried a large number of things; none do what I want.
>> GG<- c(1,2,3)
> # This one ignores the str().
>> testX <- function(X) {return(summary(X)); str(X)}
>> testX(GG)
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.0     1.5     2.0     2.0     2.5     3.0

snipped long list of unsuccessful attempts to return str results

> # This one displays both, in reverse order, with a superfluous (to my
> intentions) [[NULL]].
>> testX4 <- function(X) {list(summary(X), (str(X)))}
>> testX4(GG)
> num [1:3] 1 2 3
> [[1]]
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.0     1.5     2.0     2.0     2.5     3.0
> [[2]]
> I tried a bunch more, using the print command, etc., but nothng I  
> tried
> resulted in the output of summary() followed by the output of str().  
> And is
> there really no way to assign the output of str() -- that is to say,  
> the
> output str() normally prints to the console -- to an object?


It says: "Value   str does not return anything, for efficiency  
reasons. The obvious side effect is output to the terminal."

(Always a good idea to read the `Value` section of a help page.)
What you see at the terminal when you make an assignment is not what  
happens inside a function. The effects inside a function environment  
are only visible if they change the returned object (or cause an  
error). Most (if not all) of the output at the terminal is the result  
of cat() which also returns NULL invisibly. So str,  like most base  
plotting functions, is useful for its side-effects rather than for its  
returned value.

If you want to get rid of what you are calling "the superfluous NULL",  
then use c() rather than list:

 > testX7 <- function(X) {return(c(summary(X), str(X)))}

 > testX7(GG)

  int [1:3] 1 2 3

    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.

     1.0     1.5     2.0     2.0     2.5     3.0

> "
> I would be very greatful for any guidance you could offer.
> Sincerely, Andrew
> --
> View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3655785.html
> Sent from the R help mailing list archive at Nabble.com.
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT

More information about the R-help mailing list