Many such functions (e.g., lm and summary.lm) return an object of a
certain class and supply a separate print method to print it nicely.
Then you can compute something and print it when you want to.
Autoprinting takes care of the common case where you want it printed
right after it is computed. E.g., first define the functions
CoinTosses <- function(n) {
x <- sample(c(0,1), n, replace=TRUE)
y <- x
y[y==0] <- "T"
y[y==1] <- "H"
numHeads <- sum(x)
numTails <- n-sum(x)
p <- numHeads/n
structure(list(numHeads = numHeads, numTails = numTails, p = p),
class="CoinTosses")
}
print.CoinTosses <- function(x, ...) {
with(x, {
cat("Number of heads: ", numHeads, "\n")
cat("Number of tails: ", numTails, "\n")
cat("The proportion of heads is: ", p, "\n")
})
invisible(x) # print methods should return their first argument
}
and then use them as
> CoinTosses(10)
Number of heads: 2
Number of tails: 8
The proportion of heads is: 0.2
> z <- CoinTosses(10^5) # no autoprinting
> z # autoprint now
Number of heads: 49800
Number of tails: 50200
The proportion of heads is: 0.498
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
> -----Original Message-----
> From: [email protected] [mailto:[email protected]] On
> Behalf
> Of Sarah Goslee
> Sent: Friday, August 03, 2012 1:39 PM
> To: darnold
> Cc: [email protected]
> Subject: Re: [R] Printing a summary
>
> Hi,
>
> I find it better to return a named list so that users can do what they
> want with the results rather than just looking at them, which is all
> your approach allows. That's not incompatible with printing the
> results to the screen as well:
>
> CoinTosses <- function(n, verbose=TRUE) {
> x <- sample(c(0,1), n, replace=TRUE)
> y <- x
> y[y==0] <- "T"
> y[y==1] <- "H"
> numHeads <- sum(x)
> numTails <- n-sum(x)
> p <- numHeads/n
> if(verbose) {
> cat(cat(y,sep=""),"\n")
> cat("Number of heads: ", numHeads, "\n")
> cat("Number of tails: ", numTails, "\n")
> cat("The proportion of heads is: ", p, "\n")
> }
> list(numHeads = numHeads, numTails = numTails, p = p)
> }
>
> Sarah
>
> On Fri, Aug 3, 2012 at 4:34 PM, darnold <[email protected]> wrote:
> > All,
> >
> > Is this typical of how people will print a summary of results?
> >
> > CoinTosses <- function(n) {
> > x <- sample(c(0,1), n, replace=TRUE)
> > y <- x
> > y[y==0] <- "T"
> > y[y==1] <- "H"
> > numHeads <- sum(x)
> > numTails <- n-sum(x)
> > p <- numHeads/n
> > cat(cat(y,sep=""),"\n")
> > cat("Number of heads: ", numHeads, "\n")
> > cat("Number of tails: ", numTails, "\n")
> > cat("The proportion of heads is: ", p, "\n")
> > }
> >
> > CoinTosses(40)
>
> > Or is another technique preferred?
> >
> > Thanks.
> >
> > David
> >
> >
>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> [email protected] 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.
______________________________________________
[email protected] 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.