Re: [R] Printing upon calling a function
Steven, You need to mention what you actually did to get proper advice. Your problem is at the source. Simply put, the R interpreter does have somewhat different behavior when the program is directly typed in (or slightly indirectly as in R STUDIO) than when you ask it to open another file as a source, perhaps recursively. If you read the manual page, "source()" has lots of options. https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/source The default is to change some of the behavior with the assumption there is no user typing things in and waiting for responses. One of the options will change the behavior to make statements auto-print. Look to see if source(..., echo=TRUE) gets what you want. Obviously the "..." above is replaced with your additional code. Of course, an explicit print statement works too. You may also want to look at the print.eval option. What next? Will you now tell us you forgot that you used the sink(file="hide.txt") function in the code and wonder why it does not print to your terminal? -Original Message- From: R-help On Behalf Of Steven Yen Sent: Monday, November 30, 2020 5:55 AM To: Stefan Evert Cc: R-help Mailing List Subject: Re: [R] Printing upon calling a function No. I wrote the function so I am sure no "invisible" command was used. Strangely enough, compiling the function isto part of a package, results were NOT printed. Yes if I call the function during run, by preceding the call with a line that attach the source code: source("A:/.../R/oprobit.R") it did print. I do not understand. On 2020/11/30 下午 06:41, Stefan Evert wrote: >> On 30 Nov 2020, at 10:41, Steven Yen 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. > > Best, > Stefan __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Scanned by McAfee and confirmed virus-free. Find out more here: https://bit.ly/2zCJMrO __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
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 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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
TOPIC: Why some returned values do not automatically print. Again, not seeing the internals, my guess is the function returned not the expected but "invisible(expected)" which just marks it as not to be automatically printed. So if you want it printed, ask for it explicitly as in: print(me.probit(obj)) What you did was to copy the object and then the copy does not keep the invisibility so when invoked, gets the default print action. -Original Message- From: R-help On Behalf Of Steven Yen Sent: Monday, November 30, 2020 4:42 AM To: Jim Lemon ; r-help mailing list Subject: Re: [R] Printing upon calling a function 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) v<-me.probit(obj); v On 2020/11/30 下午 05:33, Jim Lemon wrote: > Hi Steven, > You seem to be assigning the result of me.oprobit(obj) to v instead of > printing it. By appending ";v" tp that command line, you implicitly > call "print". > > Jim > > On Mon, Nov 30, 2020 at 7:15 PM Steven Yen wrote: >> I hope I can get away without presenting a replicable set of codes >> because doing so would impose burdens. >> >> I call a function which return a data frame, with the final line >> >> return(out) >> >> In one case the data frame gets printed (similar to a regression >> printout), with simply a call >> >> me.probit(obj) >> >> In another case with a similar function, I could not get the results >> printed and the only way to print is to do the following: >> >> v<-me.oprobit(obj); v >> >> This is a puzzle, and I hope to find some clues. Thanks to all. >> >> My function looks like the following: >> >> me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, >> resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, >> Stata.mu=FALSE,testing=FALSE,digits=3){ >> ... >> return(out) # out is a data frame >> } >> >> __ >> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Scanned by McAfee and confirmed virus-free. Find out more here: https://bit.ly/2zCJMrO __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
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 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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
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 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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
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 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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
On 30/11/2020 5:41 a.m., Stefan Evert wrote: On 30 Nov 2020, at 10:41, Steven Yen 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 __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
No. I wrote the function so I am sure no "invisible" command was used. Strangely enough, compiling the function isto part of a package, results were NOT printed. Yes if I call the function during run, by preceding the call with a line that attach the source code: source("A:/.../R/oprobit.R") it did print. I do not understand. On 2020/11/30 下午 06:41, Stefan Evert wrote: On 30 Nov 2020, at 10:41, Steven Yen 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. Best, Stefan __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
> On 30 Nov 2020, at 10:41, Steven Yen 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. Best, Stefan __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
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) v<-me.probit(obj); v On 2020/11/30 下午 05:33, Jim Lemon wrote: Hi Steven, You seem to be assigning the result of me.oprobit(obj) to v instead of printing it. By appending ";v" tp that command line, you implicitly call "print". Jim On Mon, Nov 30, 2020 at 7:15 PM Steven Yen wrote: I hope I can get away without presenting a replicable set of codes because doing so would impose burdens. I call a function which return a data frame, with the final line return(out) In one case the data frame gets printed (similar to a regression printout), with simply a call me.probit(obj) In another case with a similar function, I could not get the results printed and the only way to print is to do the following: v<-me.oprobit(obj); v This is a puzzle, and I hope to find some clues. Thanks to all. My function looks like the following: me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, Stata.mu=FALSE,testing=FALSE,digits=3){ ... return(out) # out is a data frame } __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
Hi Steven, You seem to be assigning the result of me.oprobit(obj) to v instead of printing it. By appending ";v" tp that command line, you implicitly call "print". Jim On Mon, Nov 30, 2020 at 7:15 PM Steven Yen wrote: > > I hope I can get away without presenting a replicable set of codes > because doing so would impose burdens. > > I call a function which return a data frame, with the final line > > return(out) > > In one case the data frame gets printed (similar to a regression > printout), with simply a call > > me.probit(obj) > > In another case with a similar function, I could not get the results > printed and the only way to print is to do the following: > > v<-me.oprobit(obj); v > > This is a puzzle, and I hope to find some clues. Thanks to all. > > My function looks like the following: > > me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, > resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, > Stata.mu=FALSE,testing=FALSE,digits=3){ > ... > return(out) # out is a data frame > } > > __ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Printing upon calling a function
Answering you is also a burden without the reprodicible code. I'll pass on that. But I will say that mixing analysis with output in the same function is a terrible habit. Come to the functional side of coding... it is much more re-usable here. On November 30, 2020 12:14:35 AM PST, Steven Yen wrote: >I hope I can get away without presenting a replicable set of codes >because doing so would impose burdens. > >I call a function which return a data frame, with the final line > >return(out) > >In one case the data frame gets printed (similar to a regression >printout), with simply a call > >me.probit(obj) > >In another case with a similar function, I could not get the results >printed and the only way to print is to do the following: > >v<-me.oprobit(obj); v > >This is a puzzle, and I hope to find some clues. Thanks to all. > >My function looks like the following: > >me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, >resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, > Stata.mu=FALSE,testing=FALSE,digits=3){ >... >return(out) # out is a data frame >} > >__ >R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. -- Sent from my phone. Please excuse my brevity. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[R] Printing upon calling a function
I hope I can get away without presenting a replicable set of codes because doing so would impose burdens. I call a function which return a data frame, with the final line return(out) In one case the data frame gets printed (similar to a regression printout), with simply a call me.probit(obj) In another case with a similar function, I could not get the results printed and the only way to print is to do the following: v<-me.oprobit(obj); v This is a puzzle, and I hope to find some clues. Thanks to all. My function looks like the following: me.oprobit0 <- function(obj,mean=FALSE,vb.method,jindex=NA, resampling=FALSE,ndraws=100,mc.method=1,times100=TRUE, Stata.mu=FALSE,testing=FALSE,digits=3){ ... return(out) # out is a data frame } __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.