[R] Using str() in a function.

andrewH ahoerner at rprogress.org
Thu Jul 14 06:22:50 CEST 2011

Dear Peter--
You write:
>>Andrew is being seriously confused. The return(ans) is of course executed
when you get to it, returning the value of `ans` and terminating the
function. Anything after that is _ignored_. There is no such thing as a
"previous return()" affecting what str() does -- that would be like asking
whether it is legal to  marry your widow's sister... 

Right. By "previous", I was contrasting an explicit return somewhere other
than the last expression in the {} to the implicit return of the last
expression. I understand that executing a return() is the last thing a
function does.

>>str() prints last because the side effect of the preceding print()s causes
them to print before str() is ever called. 

So, what about this one:
testX3 <- function(X) {summary(X); return(str(X))}

int [1:4] 1 2 3 4

I thought this was ignoring the summary() because it evaluates the return()
first.  If it does the return(str(X)) when it encounters it, (1) why doesn't
it send the summary() to the console (I'm guessing that it is because its
output is local to the function), and (2) why doesn't it return the NULL
that str() returns to the console?

again, thanks.  --andrewH

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

More information about the R-help mailing list