It's not a bug.  It's the intended behavior.

Sprintf by itself returns a formatted character string and doesn't print 
anything.  Without the return statement, the function returns the string, which 
the interactive processor then prints (since that's what it does when you 
simply present it with an object).

To get the same behavior inside the function and still return the value 'a', 
just do this:

x <- function() { a <- 888
+ print(sprintf("xxx %s", a) )
+ return(a) }

That just makes explicit what the combination of function + interaction is 
doing implicitly.

Good luck!

-- 
Jeremy Raw, PE, AICP 
Senior Modeling Systems Analyst 
Virginia DOT, Transportation and Mobility Planning 
jeremy....@vdot.virginia.gov / 804-786-0998

-----Original Message-----
From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
Behalf Of Albert-Jan Roskam
Sent: Tuesday, April 27, 2010 09:29 AM
To: R Mailing List
Subject: [R] sprintf() and return() oddity

Hi,
 
If I use sprintf and return inside a function, sprintf doesn't print anything 
anymore. See the non-sense example below.
 
> x <- function() { a <- 888
+ sprintf("xxx %s", a) }
> x()
[1] "xxx 888"
> x <- function() { a <- 888
+ sprintf("xxx %s", a) 
+ return(a) }
> x()
[1] 888

Is this a bug?

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public 
order, irrigation, roads, a fresh water system, and public health, what have 
the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


      
        [[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.

Reply via email to