[R] Printing upon calling a function

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Mon Nov 30 18:04:24 CET 2020

On 30/11/2020 11:51 a.m., Steven Yen wrote:
> Thanks to all. Presenting a large-scale, replicable example can be a
> burden to the READERs which was why I was reluctant.

You shouldn't post a large scale reproducible example, you should 
simplify it to just the essentials.  Often in doing that you will find 
some error, and don't need to post at all.

Duncan Murdoch

> I am embarrassed to report that after having to restart Windows after
> the system hang on something unrelated, the issue was resolved and
> printing was normal. I bet it had nothing to do with the R function.
> Problem caused by my Windows system memory or something. This sometimes
> yes, sometimes no situation makes i thard to pinpoint the problem and
> present a replicable example. I am OK now. Thanks to all.
> On 2020/11/30 下午 07:21, Duncan Murdoch wrote:
>> 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