[R] R equiv of #ifdef?

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Jun 7 11:38:29 CEST 1999


Bill Simpson <wsimpson at gcal.ac.uk> writes:

> I was wondering if R has an equivalent of #ifdef.
> I want the code to check if a function has been sourced in yet, and if
> not, then source it in. (I know of replace(), which works for
> packages/libraries)
> 
> Here is what I have come up with.
> 
> my.objects<-objects()
> if(my.objects[my.objects=="thingy2"]!="thingy2")
> 	source("~/papers/speed/junk.r")
> 
> junk.r contains the code for function thingy2().
> This doesn't work: "missing value where logical needed"
> The error message puzzles me.
> 
> >my.objects[my.objects=="thingy2"]!="thingy2"
> logical(0)

How about exists("thingy2")?

The error message is perhaps not perfectly clear, you need not only a
logical but a non-missing logical of positive length:

> if(logical(0))2
Error in if (logical(0)) 2 : missing value where logical needed
> if(NULL)2
Error in if (NULL) 2 : missing value where logical needed
> if(as.logical(NA))2
Error in if (as.logical(NA)) 2 : missing value where logical needed

You're getting a zero-length vector from the
my.objects[my.objects=="thingy2"] construction, which is compared
elementwise with "thingy2".

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list