I would like to suppressed printing of retrievable results in a procedure and to print only when retrieved. In line 10 below I call procedure "try" and get matrices A,B,C all printed upon a call to the procedure. I get around this unwanted printing by calling with v<-try(A,B) as in line 11. Any way to suppress printing of the retrievable results listed in the structure command? Thank you, and Merry Christmas to all. A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) B<-diag(4) try<-function(A,B){ C<-A+B cat("\nC:\n"); print(C) structure(list(A=A,B=B,C=C)) } try(A,B) # line 10 v<-try(A,B) # line 11 -- styen at ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]]
I'm a bit confused about what you actually want, but I think invisible() might be the answer. Note that there's already a base function try() so that's not a great name for test functions. Sarah On Tue, Dec 25, 2018 at 8:47 AM Steven Yen <styen at ntu.edu.tw> wrote:> I would like to suppressed printing of retrievable results in a > procedure and to print only when retrieved. > > In line 10 below I call procedure "try" and get matrices A,B,C all > printed upon a call to the procedure. I get around this unwanted > printing by calling with v<-try(A,B) as in line 11. > > Any way to suppress printing of the retrievable results listed in the > structure command? Thank you, and Merry Christmas to all. > > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try<-function(A,B){ > C<-A+B > cat("\nC:\n"); print(C) > structure(list(A=A,B=B,C=C)) > } > > try(A,B) # line 10 > v<-try(A,B) # line 11 > > -- > styen at ntu.edu.tw (S.T. Yen) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Sarah Goslee (she/her) http://www.sarahgoslee.com [[alternative HTML version deleted]]
You can use `capture.output`, but a far, far better solution is to remove the output statements from your computation functions entirely and let the caller decide whether to print the results. You can, for example, add a `debug` parameter to the function, and if true it can return a list of as many intermediate results as you like that you can examine as you wish. Of course, if debugging is your goal then learning to use the debug function to mark functions for single-stepping as needed is even better. But no matter what, making functions that do both computation and output is really poor practice... do one or the other. On December 25, 2018 5:42:13 AM PST, Steven Yen <styen at ntu.edu.tw> wrote:>I would like to suppressed printing of retrievable results in a >procedure and to print only when retrieved. > >In line 10 below I call procedure "try" and get matrices A,B,C all >printed upon a call to the procedure. I get around this unwanted >printing by calling with v<-try(A,B) as in line 11. > >Any way to suppress printing of the retrievable results listed in the >structure command? Thank you, and Merry Christmas to all. > > >A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) >B<-diag(4) > >try<-function(A,B){ > C<-A+B > cat("\nC:\n"); print(C) >structure(list(A=A,B=B,C=C)) >} > >try(A,B) # line 10 >v<-try(A,B) # line 11-- Sent from my phone. Please excuse my brevity.
Thanks Sarah. Below, replacing "structure" with "invisible" does wonders--that serves my need. What I want is quite simple - I call a procedure and it does two things: (1) display results for all; (2) save retrievable results for use in further analysis, e.g., in knitr. Earlier, with "structure" (or with results<-list(...)) it spits out the main results, with components repeated (printed) in a painfully long list. Yet, as I said, calling with foo<-try(...) prints the main results with the list suppressed. I am just looking for option to NOT have to call with foo<- always. There must be more ways to do this, but I am happy with invisible. Thanks again. On 12/25/2018 11:10 PM, Sarah Goslee wrote:> I'm a bit confused about what you actually want, but I think > invisible() might be the answer. > > Note that there's already a base function try() so that's not a great > name for test functions. > > Sarah > > On Tue, Dec 25, 2018 at 8:47 AM Steven Yen <styen at ntu.edu.tw > <mailto:styen at ntu.edu.tw>> wrote: > > I would like to suppressed printing of retrievable results in a > procedure and to print only when retrieved. > > In line 10 below I call procedure "try" and get matrices A,B,C all > printed upon a call to the procedure. I get around this unwanted > printing by calling with v<-try(A,B) as in line 11. > > Any way to suppress printing of the retrievable results listed in the > structure command? Thank you, and Merry Christmas to all. > > > A<-matrix(rpois(16,lambda=5),nrow=4,byrow=T) > B<-diag(4) > > try<-function(A,B){ > C<-A+B > cat("\nC:\n"); print(C) > structure(list(A=A,B=B,C=C)) > } > > try(A,B) # line 10 > v<-try(A,B) # line 11 > > -- > styen at ntu.edu.tw <mailto:styen at ntu.edu.tw> (S.T. Yen) > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org <mailto:R-help at 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. > > -- > Sarah Goslee (she/her) > http://www.sarahgoslee.com-- styen at ntu.edu.tw (S.T. Yen) [[alternative HTML version deleted]]