[R] sprintf() and return() oddity
Raw, Jeremy, P.E.
Jeremy.Raw at VDOT.Virginia.gov
Tue Apr 27 15:45:47 CEST 2010
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.raw at vdot.virginia.gov / 804-786-0998
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at 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]]
More information about the R-help
mailing list