Skip to content

no implicit call of the print function within loops?

2 messages · Mark Heckmann, Greg Snow

#
Dear R-users,

I wonder why some functions produce output when they are called (I suppose
due to an implicit call of the print function) but within a loop they do
not:

attach(anscomce)
exp <- parse(text= "lm(x1 ~ y1)")
eval(exp)

Here the print() function seems to be called implicitly.
If I do the same within a for-loop, it is not.

for (i in c(1)){
   eval(exp)
}

I know that I have to wrap it into a print function so it would work. 
But why is that so? In the eval() help I don't find any clues. 
As this happens with other functions as well, I would like to understand the
causes and thus
avoid some future mistakes.

TIA,
Mark
#
The general rule is that when you type something at the command line and that something returns a value, if you do not tell it what to do with the value, and the value has not been made invisible, then the value is printed.

If you do:
You will not see the output printed since you tell it what to do with the return value from eval, but without the assignment, it is not told what to do and so does the print.  This is not unique to the eval function, but true for anything that returns a visible value, compare:
The implicit print does not happen inside of loops, batch processing, inside of functions, and probably a few other places.

Hope this helps,

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111