[R] Printing upon calling a function
Steven Yen
@tyen @end|ng |rom ntu@edu@tw
Mon Nov 30 17:51:57 CET 2020
Thanks to all. Presenting a large-scale, replicable example can be a
burden to the READERs which was why I was reluctant.
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