[R] conditional function definition?

Martin Maechler maechler at stat.math.ethz.ch
Sat Sep 20 18:54:26 CEST 2003


>>>>> "Spencer" == Spencer Graves <spencer.graves at pdf.com>
>>>>>     on Sat, 20 Sep 2003 08:12:52 -0700 writes:

    Spencer> Have you considered:
    >> exists("slice.index")
    Spencer> [1] TRUE

    Spencer>        In other circumstances, I've tested various
    Spencer> components of "version".

I'd recommend the latter, since there could be "slice.index"
lying around somewhere else.

I have the following in the cluster package's  R/zzz.R  file:
--------------------------------------------------------------------------------
    ## for R versions < 1.7:
    if(paste(R.version$major, R.version$minor, sep=".") < 1.7) {

	force <- function(x) x

	## for R versions < 1.6:
	if(paste(R.version$major, R.version$minor, sep=".") < 1.6) {
	    stop <- function (..., call. = TRUE)
		.Internal(stop(if (nargs() > 0) paste(..., sep = "")))

	    ## for R versions < 1.5
	    if(paste(R.version$major, R.version$minor, sep=".") < 1.5)
		## cheap substitute, used in silhouette.default()
		colSums <- function(x) apply(x, 2, sum)

    ### NOTE: From cluster 1.7.0, we require at least R 1.4

	}# versions < 1.6

    }# versions < 1.7
--------------------------------------------------------------------------------

    Spencer> hope this helps.  spencer graves

    Spencer> (Ted Harding) wrote:

    >> Hi Folks,
    >> 
    >> What is the best way to avoid a function being read in
    >> anew (and masking an exiting function) when a definition
    >> of it has already been established in R?
    >> 
    >> Reason: Fernando Tusell and I are working up Schafer's
    >> 'CAT' for R (basically done now, just needs some cosmetic
    >> tidying up).
    >> 
    >> This uses a function 'slice.index', present in S but not
    >> in the versions of R we were working with at the time. So
    >> we put in a definition (copied from R-help ... ).
    >> 
    >> However, it seems that slice.index is now in "base" in
    >> latest versions of R. So it would seem a bit silly to
    >> read it in anew.  Nevertheless, probably we should keep
    >> it in for the sake of people still using older versions
    >> of R who would not have it.
    >> 
    >> So what's the best method to do
    >> 
    >> if( some test for function slice.index absent ) {
    >> slice.index<-function(....){....}  }
    >> 
    >> ??
    >> 
    >> Thanks, Ted.
    >> 
    >> 
    >> --------------------------------------------------------------------
    >> E-Mail: (Ted Harding) <Ted.Harding at nessie.mcc.ac.uk>
    >> Fax-to-email: +44 (0)870 167 1972 Date: 20-Sep-03 Time:
    >> 15:07:59 ------------------------------ XFMail
    >> ------------------------------
    >> 
    >> ______________________________________________
    >> R-help at stat.math.ethz.ch mailing list
    >> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
    >> 
    >> 

    Spencer> ______________________________________________
    Spencer> R-help at stat.math.ethz.ch mailing list
    Spencer> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list