GenericFunctions           package:methods           R Documentation

_T_o_o_l_s _f_o_r _M_a_n_a_g_i_n_g _G_e_n_e_r_i_c _F_u_n_c_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     The functions documented here manage collections of methods
     associated with a generic function, as well as providing
     information about the generic functions themselves.

_U_s_a_g_e:

     isGeneric(f, where, fdef, getName = FALSE)
     isGroup(f, where, fdef)
     removeGeneric(f, where)

     dumpMethod(f, signature, file, where, def)
     findFunction(f, generic = TRUE, where = topenv(parent.frame()))
     dumpMethods(f, file, signature, methods, where)
     signature(...)

     removeMethods(f, where = topenv(parent.frame()), all = TRUE)
     setReplaceMethod(f, ..., where = topenv(parent.frame()))

     getGenerics(where, searchForm = FALSE)
     allGenerics(where, searchForm = FALSE)
     callGeneric(...)

_A_r_g_u_m_e_n_t_s:

       f: The character string naming the function. 

   where: The environment, namespace, or search-list position from
          which to search for objects.  By default, start at the
          top-level environment of the calling function, typically the
          global environment (i.e., use the search list), or the
          namespace of a package from which the call came.  It is
          important to supply this argument when calling any of these
          functions indirectly.  With package namespaces, the default
          is likely to be wrong in such calls.

signature: The class signature of the relevant method.  A signature is
          a named or unnamed vector of character strings.  If named,
          the names must be formal argument names for the generic
          function.  If 'signature' is unnamed, the default is to use
          the first 'length(signature)' formal arguments of the
          function. 

    file: The file on which to dump method definitions. 

     def: The function object defining the method; if omitted, the
          current method definition corresponding to the signature. 

     ...: Named or unnamed arguments to form a signature.

 generic: In testing or finding functions, should generic functions be
          included.  Supply as 'FALSE' to get only non-generic
          functions.

    fdef: Optional, the generic function definition.

          Usually omitted in calls to 'isGeneric'

 getName: If 'TRUE', 'isGeneric' returns the name of the generic.  By
          default, it returns 'TRUE'. 

 methods: The methods object containing the methods to be dumped.  By
          default, the methods defined for this generic (optionally on
          the specified 'where' location). 

     all: in 'removeMethods', logical indicating if all (default) or
          only the first method found should be removed.

searchForm: In 'getGenerics', if 'TRUE', the 'package' slot of the
          returned result is in the form used by 'search()', otherwise
          as the simple package name (e.g, '"package:base"' vs
          '"base"'). 

_S_u_m_m_a_r_y _o_f _F_u_n_c_t_i_o_n_s:

     '_i_s_G_e_n_e_r_i_c': Is there a function named 'f', and if so, is it a
          generic?

          The 'getName' argument allows a function to find the name
          from a function definition.  If it is 'TRUE' then the name of
          the generic is returned, or 'FALSE' if this is not a generic
          function definition.

          The behavior of 'isGeneric' and 'getGeneric' for primitive
          functions is slightly different.  These functions don't exist
          as formal function objects (for efficiency and historical
          reasons), regardless of whether methods have been defined for
          them.  A call to 'isGeneric' tells you whether methods have
          been defined for this primitive function, anywhere in the
          current search list, or in the specified position 'where'. 
          In contrast, a call to 'getGeneric' will return what the
          generic for that function would be, even if no methods have
          been currently defined for it.


     '_r_e_m_o_v_e_G_e_n_e_r_i_c', '_r_e_m_o_v_e_M_e_t_h_o_d_s': Remove all the methods for the
          generic function of this name.  In addition, 'removeGeneric'
          removes the function itself; 'removeMethods' restores the
          non-generic function which was the default method.   If there
          was no default method, 'removeMethods' leaves a generic
          function with no methods.

     '_s_t_a_n_d_a_r_d_G_e_n_e_r_i_c': Dispatches a method from the current function
          call for the generic function 'f'.  It is an error to call
          'standardGeneric' anywhere except in the body of the
          corresponding generic function.

          Note that 'standardGeneric' is a primitive function in the
          'base' package for efficiency  reasons, but rather documented
          here where it belongs naturally.

     '_d_u_m_p_M_e_t_h_o_d': Dump the method for this generic function and
          signature.

     '_f_i_n_d_F_u_n_c_t_i_o_n': return a list of either the positions on the
          search list, or the current top-level environment, on which a
          function object for 'name' exists.  The returned value is
          _always_ a list, use the first element to access the first
          visible version of the function.  See the example.

          _NOTE:_ Use this rather than 'find' with 'mode="function"',
          which is not as meaningful, and has a few subtle bugs from
          its use of regular expressions.  Also, 'findFunction' works
          correctly in the code for a package when attaching the
          package via a call to 'library'.

     '_d_u_m_p_M_e_t_h_o_d_s': Dump all the methods for this generic.

     '_s_i_g_n_a_t_u_r_e': Returns a named list of classes to be matched to
          arguments of a generic function.

     '_g_e_t_G_e_n_e_r_i_c_s': Returns the names of the generic functions that
          have methods defined on 'where'; this argument can be an
          environment or an index into the search list.  By default,
          the whole search list is used.

          The methods definitions are stored with package qualifiers;
          for example, methods for function '"initialize"' might refer
          to two different functions of that name, on different
          packages.  The package names corresponding to the method list
          object are contained in the slot 'package' of the returned
          object.  The form of the returned name can be plain (e.g.,
          '"base"'), or in the form used in the search list
          ('"package:base"') according to the value of 'searchForm'

     '_c_a_l_l_G_e_n_e_r_i_c': In the body of a method, this function will make a
          call to the current generic function.  If no arguments are
          passed to 'callGeneric', the arguments to the current call
          are passed down; otherwise, the arguments are interpreted as
          in a call to the generic function.

_D_e_t_a_i_l_s:

     '_s_e_t_G_e_n_e_r_i_c': If there is already a non-generic function of this
          name, it will be used to define the generic unless 'def' is
          supplied, and the current function will become the default
          method for the generic.

          If 'def' is supplied, this defines the generic function, and
          no default method will exist (often a good feature, if the
          function should only be available for a meaningful subset of
          all objects).

          Arguments 'group' and 'valueClass' are retained for
          consistency with S-Plus, but are currently not used.

     '_i_s_G_e_n_e_r_i_c': If the 'fdef' argument is supplied, take this as the
          definition of the generic, and test whether it is really a
          generic, with 'f' as the name of the generic.  (This argument
          is not available in S-Plus.)

     '_r_e_m_o_v_e_G_e_n_e_r_i_c': If 'where' supplied, just remove the version on
          this element of the search list; otherwise, removes the first
          version encountered.

     '_s_t_a_n_d_a_r_d_G_e_n_e_r_i_c': Generic functions should usually have a call to
          'standardGeneric' as their entire body.  They can, however,
          do any other computations as well.

          The usual 'setGeneric' (directly or through calling
          'setMethod') creates a function with a call to
          'standardGeneric'.

     '_d_u_m_p_M_e_t_h_o_d': The resulting source file will recreate the method.

     '_f_i_n_d_F_u_n_c_t_i_o_n': If 'generic' is 'FALSE', ignore generic functions.

     '_d_u_m_p_M_e_t_h_o_d_s': If 'signature' is supplied only the methods
          matching this initial signature are dumped.  (This feature is
          not found in S-Plus:  don't use it if you want
          compatibility.)

     '_s_i_g_n_a_t_u_r_e': The advantage of using 'signature' is to provide a
          check on which arguments you meant, as well as clearer
          documentation in your method specification.  In addition,
          'signature' checks that each of the elements is a single
          character string.

     '_r_e_m_o_v_e_M_e_t_h_o_d_s': Returns 'TRUE' if 'f' was a generic function,
          'FALSE' (silently) otherwise.

          If there is a default method, the function will be
          re-assigned as a simple function with this definition.
          Otherwise, the generic function remains but with no methods
          (so any call to it will generate an error).  In either case,
          a following call to 'setMethod' will consistently
          re-establish the same generic function as before.

_R_e_f_e_r_e_n_c_e_s:

     The R package 'methods' implements, with a few exceptions, the
     programming interface for classes and methods in the book
     _Programming with Data_ (John M. Chambers, Springer, 1998), in
     particular sections 1.6, 2.7, 2.8, and chapters 7 and 8.

     While the programming interface for the methods package follows
     the reference, the R software is an original implementation, so
     details in the reference that reflect the S4 implementation may
     appear differently in R.  Also, there are extensions to the
     programming interface developed more recently than the reference. 
     For a discussion of details and ongoing development, see the web
     page <URL: http://developer.r-project.org/methodsPackage.html> and
     the pointers from that page.

_S_e_e _A_l_s_o:

     'getMethod' (also for 'selectMethod'), 'setGeneric', 'setClass',
     'showMethods'

_E_x_a_m_p_l_e_s:

     ## get the function "myFun" -- throw an error if 0 or > 1 versions visible:
     findFuncStrict <- function(fName) {
       allF <- findFunction(fName)
       if(length(allF) == 0)
         stop("No versions of ",fName," visible")
       else if(length(allF) > 1)
         stop(fName," is ambiguous: ", length(allF), " versions")
       else
         get(fName, allF[[1]])
     }

     try(findFuncStrict("myFun"))# Error: no version
     lm <- function(x) x+1
     try(findFuncStrict("lm"))#    Error: 2 versions
     findFuncStrict("findFuncStrict")# just 1 version
     rm(lm)

