Hi,
Every _expression_ in S has a value. When there is no else statement and the condition is FALSE, the value of the if is NULL.
This makes this kind of construct possible:
x <- 2.5
a <- if( x < 3 ) 4 else 5
a <- if( x < 2 ) 4
a <- if(!X) cat("hello")
I'm afraid there is little you can do about not returning that NULL. If it is the last statement of a function, it will get returned. The usual trick then is to insert a call to invisible before the end of the function.
The other way would be to invert the call to cat and the test to get this kind of call. (The readability might be discussed)
cat( if(!X) "hello" )
Hope that helps,
Romain
BTW, is there any plans to add statement modifiers as in Perl. This way we could write:
cat( "hello") unless X
-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu on behalf of Dennis Fisher
Sent: Fri 19/10/2007 15:00
To: s-news@lists.biostat.wustl.edu
Subject: [S] Splus returns NULL
Colleagues
This is probably remarkably simple but I cannot find the answer.
When a conditional statement tests as negative, Splus (version 7.0.0
running on RedHat Linux) returns NULL.
for example:
X <- T
if (!X) cat("hello")
returns NULL. I have tested options(warn=-1) and options(echo=F) to
no avail. In R, the same commands return nothing.
Any ideas how to prevent the NULL returns?
Dennis
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-415-564-2220
www.PLessThan.com
|