[R] Printing upon calling a function

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Nov 30 12:00:48 CET 2020

On 30/11/2020 5:41 a.m., Stefan Evert wrote:
>> On 30 Nov 2020, at 10:41, Steven Yen <styen using ntu.edu.tw> wrote:
>> Thanks. I know, my point was on why I get something printed by simply doing line 1 below and at other occasions had to do line 2.
>> me.probit(obj)
> That means the return value of me.probit() has been marked as invisible, so it won't auto-print.  You have to use an explicit print
> 	print(me.probit(obj))
> or use your work-around to convince R that you actually meant to print the output.
> If you dig through the full code of me.probit(), you'll probably find the function invisible() called somewhere.

I think you misread his post.  "me.probit(obj)" on its own *did* print. 
  It was when he assigned it to a variable using "v <- me.probit(obj)" 
that it didn't.  Assignments are almost always invisible in R.

The other thing that people sometimes find confusing is that evaluating 
expressions that are visible are the top level doesn't make them print 
when they are nested in a block of code.  Usually this happens in a 
function, e.g. typing a number normally makes it visible, but

f <- function() {

doesn't print 1, it only prints 2, and that happens because 2 is the 
return value of the function.

Duncan Murdoch

More information about the R-help mailing list