[R] Printing upon calling a function

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


By not posting a reproducible example, you're wasting everyone's time.

Duncan Murdoch

On 30/11/2020 6:06 a.m., Steven Yen wrote:
> No, sorry. Line 1 below did not print for me and I had to go around and
> do line 2 to print:
> 
> me.probit(obj)
> 
> v<-me.probit(obj); v
> 
> A puzzle.
> 
> 
> On 2020/11/30 下午 07:00, Duncan Murdoch wrote:
>> 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() {
>>    1
>>    2
>> }
>> f()
>>
>> 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