[R] Using str() in a function.

andrewH ahoerner at rprogress.org
Thu Jul 14 01:31:37 CEST 2011


Thanks, David & Dennis, that is very helpful.

Let me state what I think I have learned, to see if I understand it
correctly. Then I have two remaining questions.

If a function contains more than one expression in its {}, it always returns
the value of the last evaluated expression in its definition, and only the
last object -- unless you previously use the return() function on an object
before the last expression, in which case, the value of that expression is
returned instead. And in either case, explicit or implicit return(), the
returned expression is evaluated, and returned, first -- before any other
expressions are evaluated, and any side effects also occur before any other
expressions are evaluated. (Though I am unsure in what order expressions are
evaluated if  objects in the returned expression are defined by other
expressions before it in the function.  The chain of evaluation -- and of
any side effects of that evaluation -- propogates backwards, maybe?).  

The print() command inside a function sends the object it contains to the
currently-defined printer, as a side-effect, without returning it.  The
difference between return() and print() is that if something is returned, R
checks to see if the value of the function is assigned or otherwise nested
in a larger evaluated expression. Is so, a copy is moved to the assigned
object and the original is deleted. If not, it is printed to the current
device and then deleted. If you print() it, it does not check for assignment
or use before sending it to the printer and deleting it.

A lot of functions, e.g. str(), have as their explicit or implict return an
expression which does not create an object. In this case, the function
returns a NULL. If you do not want to print the NULL or other returned
object, you make the returned argument invisible().

But there are still things here I do not understand.  The function that
Dennis Murphy provided does print the str() output last instead of first,
because its final expression is invisible() rather than str(). But, it still
prints out (and returns - I checked) a NULL.  e.g.

 GG<-c(1:5)
 testXa <- function(X) {
     print(summary(X))
     print(str(X))
     invisible()     # returns nothing
  }
> testXa(GG)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       2       3       3       4       5 
 int [1:5] 1 2 3 4 5
NULL
 
 # Here is my latest version, of the function, which does exactly what I
want:
 testXf <- function(X) {
     print("Summary:"); print(summary(X))
     print("Structure:");  invisible(str(X))
  }
 testXf(GG)
[1] "Summary:"
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
      1       2       3       3       4       5 
[1] "Structure:"
 int [1:5] 1 2 3 4 5

So, two questions:
1. In Dennis's function, the str() results are printed last because they are
no longer returned, as invisible() is now the last expression. But why does
his function still print a visible NULL?
2. My function, above, makes the NULL value returned by str() invisible. But
invisible(str(X)) is the last expression evaluated, so why does the
side-effect printing of str() results happen last instead of first?

and thanks again!
andrewH

--
View this message in context: http://r.789695.n4.nabble.com/Using-str-in-a-function-tp3655785p3666339.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list