Re: [R] Printing a title for an object in the console when writing a function XXXX

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Sat, 07 May 2011 09:42:18 -0400

On May 7, 2011, at 4:16 AM, Dan Abner wrote:

> Hello everyone,
>
> What is the best way to have R print a title and then skip a line
> (both
> without the [1] line preffix) when writing a function that prints an
> object
> (to the console)? Simplified example:
>
>
> fn3<-function(x,y){
>
> c1<-cov(x,y)
>
> print(c1)}

?cat

fn3<-function(x,y){
cat("Please examine this beautiful title\n") c1<-cov(x,y)
cat(" ... here is your covariance ... \n", c1)}

>
> data1<-data.frame(age=rnorm(50),weight=rnorm(50))
>
> #### attach(data1)
> # Do not use attach, especially with objects named 'data1' and
> columns named 'weight'

# Here is what I saw on my system
#> attach(data1)
#The following object(s) are masked _by_ '.GlobalEnv':
#
#   weight
# there was already a weight object in the workspace and the
# interpreter did not know which one to use
# and it was telling me it had 'decided' to use the other one
# So .... instead use with() as below:

 > with(data1, fn3(age,weight) )
# output ...
Please examine this beautiful title
  ... here is your covariance ...
  0.03096163

> fn3(age,weight)
>
> =====
>
> How do I have R print a nicely formatted title when printing c1 to the
> console?
>

(And Dan, if you are a member of this list, then be advised that putting 'XXXX' in your subject line is going to get you email held by the spambot that 'thinks' you are advertising something more interesting than statistics.)

David Winsemius, MD
West Hartford, CT



R-help_at_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. Received on Sat 07 May 2011 - 13:47:19 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 07 May 2011 - 14:00:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive