Re: [R] Return one value, print another
Joshua Wiley-2 wrote: I am stuck trying to figure out how to make a function return one value and print another. Here is an example function: .. see below My current solution has been to round the variables before putting The easiest way out, e.g. for a lecture, is to use options(digits=x), which is global but can be reset. Otherwise, you could assign a class to your returned output, and define print.myclass. Probably a bit too much work for what you want to achieve. See, for example, the code of print.htest. Dieter eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - list(xbar, sdx) names(value) - c(Mean of X, SD of X) value } options(digits=2) eg(rnorm(10)) -- View this message in context: http://n4.nabble.com/Return-one-value-print-another-tp1590248p1590260.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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] Return one value, print another
Hmm... do something like that, no need to change the global option (I used a named vector instead of a list, it's more convenient): eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - c(xbar, sdx) names(value) - c(Mean of X, SD of X) print(round(value,digits)); return(invisible(value))} On Fri, Mar 12, 2010 at 8:14 AM, Joshua Wiley jwiley.ps...@gmail.comwrote: Dear R users, I am stuck trying to figure out how to make a function return one value and print another. Here is an example function: ## eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - list(xbar, sdx) names(value) - c(Mean of X, SD of X) return(value)} ## My current solution has been to round the variables before putting them into the list. Since it can go up to 22 digits, this is fine for my basic needs. However, my goal is for assignments to have full precision, but the screen printout to be rounded to digits. I have looked through ?return ?cat ?print. Can anyone suggest where I can learn how to do this (help pages, books, etc.)? Thanks in advance, Josh -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] Return one value, print another
Miguel Porto wrote: Hmm... do something like that, no need to change the global option (I used a named vector instead of a list, it's more convenient): eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - c(xbar, sdx) names(value) - c(Mean of X, SD of X) print(round(value,digits)); return(invisible(value))} Would be ok, but Josh explicitly had no print in the function, so one could assume that he wanted full control. Dieter -- View this message in context: http://n4.nabble.com/Return-one-value-print-another-tp1590248p1590563.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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] Return one value, print another
Yeah that's right; in that case implementing the print.myclass as you say would be the best option. Miguel On Fri, Mar 12, 2010 at 2:17 PM, Dieter Menne dieter.me...@menne-biomed.dewrote: Miguel Porto wrote: Hmm... do something like that, no need to change the global option (I used a named vector instead of a list, it's more convenient): eg - function(x, digits=4) { xbar - mean(x) sdx - sd(x) value - c(xbar, sdx) names(value) - c(Mean of X, SD of X) print(round(value,digits)); return(invisible(value))} Would be ok, but Josh explicitly had no print in the function, so one could assume that he wanted full control. Dieter -- View this message in context: http://n4.nabble.com/Return-one-value-print-another-tp1590248p1590563.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list 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] Return one value, print another
Dieter: I had considered creating a special class, but you are right that it was more than I (really) wanted to do. Setting the global digits option is an excellent suggestion. Thank you. Right now I am working with simple functions mostly to save myself a few steps. Miguel: I did not use print() because I was under the (mistaken) impression that functions would only return the last expression evaluated or the contents of return(). So I thought if I used print() it would be ignored unless it came outside the function, and I did not want to create global objects. Also I used a list() just following the suggestion from the return help page, but I will look at named vectors also. So thank you for that idea too. Thanks again. Josh -- Joshua Wiley Senior in Psychology University of California, Riverside http://www.joshuawiley.com/ __ R-help@r-project.org mailing list 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.