Skip to content
Prev 171657 / 398503 Next

error in dat

Prof Brian Ripley wrote:
are you sure it is not your explanation that is misleading?  as far as i
can see, bindings for function objects are made in environments in just
the same way as for other kinds of objects. 

when r looks for a function named 'foo', it checks, starting from the
current environment, for bindings for the name 'foo';  when it finds
one, it examines the corresponding value for its being a function
(roughly), and either successfully returns the value, or proceeds with
the enclosing environment.  this is what can be seen in the sources
(simplified):

   // starting at line 1223 in src/main/envir.c as of r48000
   while (rho != R_EmptyEnv) {
      ...
      vl = findVarInFrame3(rho, symbol, TRUE);
      if (vl != R_UnboundValue) {
         ...
         if (TYPEOF(vl) == CLOSXP || TYPEOF(vl) == BUILTINSXP ||
TYPEOF(vl) == SPECIALSXP)
            return (vl);
         ...
      }
   ...
   }

clearly, appropriately named values *are* found and examined even if
they're not functions.  this is also acknowledged in the following comment:

   // starting at line 1208 in src/main/envir.c as of r48000
   findFun

   Search for a function in an environment This is a specially modified
   version of findVar which ignores values its finds if they are not
   functions.

note the 'finds'.

back to the original issue, your "it does not find the data frame when
looking for a function." is *wrong*.  r *does* find the data frame; r
ignores it because it is not a function, but it ignores it only *after*
the data frame has been *found* and *examined*.  it searches further,
finds no function named 'dat', and complains.

to a naive user, the error message discussed above may be misleading. 
if it said, e.g., that none of the objects named 'dat' is a function, it
might be easier for the user to find out what's wrong.

vQ