[R] Returning from a function

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 24 16:45:10 CEST 2005


Liaw, Andy wrote:
> Use invisible(NULL) as the last line. 

Or nothing at all, in which case the result of the last executed line 
will be returned.  If your function does plotting, the last line is 
probably one of the graphics functions, and they typically return 
invisible results.

Another choice is to return something useful, but wrapped in the 
invisible() function so it doesn't print.  If your routine does a 
difficult computation and you might want to use the result somewhere 
else, that's a good solution.  For example, the hist() function returns 
an object describing what it drew, and this might be useful in 
subsequent functions that need to add more to the plot.

Duncan Murdoch


> 
> Andy
> 
> 
>>From: Laura Holt
>>
>>Hello again.
>>
>>I have a function that plots a series and adds some 
>>interesting items to the 
>>plot.
>>
>>Fair enough.
>>
>>The last statement in the function is
>>return()
>>
>>When the function is executed, NULL appears at the end, which is ok.
>>Is there any way to prevent NULL from appearing, or is that 
>>just as it 
>>should be, please?
>>
>>thanks again,
>>Shamefacedly,
>>Laura Holt
>>R Version 2.1.0 Windows
>>mailto: lauraholt_983 at hotmail.com




More information about the R-help mailing list