[R] Using str() in a function.

Dennis Murphy djmuser at gmail.com
Sat Jul 9 15:56:25 CEST 2011


Hi:

Is this what you're after?

testX <- function(X) {
     print(summary(X))
     print(str(X))
     invisible()     # returns nothing
 }
testX(1:10)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   1.00    3.25    5.50    5.50    7.75   10.00
 int [1:10] 1 2 3 4 5 6 7 8 9 10
NULL

See inline..

On Sat, Jul 9, 2011 at 1:20 AM, andrewH <ahoerner at rprogress.org> 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

Yes, because you asked it to return summary(X).

>
> # So does this one.
>> testX2 <- function(X) {return(summary(X)); return(str(X))}
>> testX2(GG)
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.0     1.5     2.0     2.0     2.5     3.0

Only the first return() is implemented. (Hint: return() is not always
a good way to exit a function.)
>
> # On the other hand, this one ignores the summary()
>> testX3 <- function(X) {summary(X); return(str(X))}
>> testX3(GG)
>  num [1:3] 1 2 3

You asked it to return str(X) - it did.
>
> # 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]]
> NULL

str() doesn't save its output - it only prints. Here's an example:

> u <- str(1:10)
 int [1:10] 1 2 3 4 5 6 7 8 9 10
> u
NULL

This is why it prints in front of the list but doesn't get saved into the list.
>
> # Now we are back to ignoring the str().
>> testX5 <- function(X) {list(return(summary(X)), (str(X)))}
>> testX5(GG)
>   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
>    1.0     1.5     2.0     2.0     2.5     3.0

return(summary(X)) is superfluous; the list() construct is an implicit
return() object.
>
> # This does the same as testX4().
>> testX6 <- function(X) {return(list(summary(X), (str(X))))}
>> testX6(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]]
> NULL

This is the same as testX4.
>
> 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?
>
> I would be very greatful for any guidance you could offer.

You could always read An Introduction to R - specifically, Chapter 10,
writing your own functions. If you look closely, you'll notice that
return() is never used in any of the examples in that chapter. It's
not 'wrong' to use return(), but it's locked you into a certain
mindset you need to get out of. return() is one way of passing output
to the object to which the function call is assigned, but your
apparent goal was 'display', for which print() is perfectly adequate.

HTH,
Dennis
>
> 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.
>



More information about the R-help mailing list