From therneau at mayo.edu  Fri Nov  6 15:20:57 2015
From: therneau at mayo.edu (Therneau, Terry M., Ph.D.)
Date: Fri, 06 Nov 2015 08:20:57 -0600
Subject: [Rd] Puzzled by eval
In-Reply-To: <563CB04C.6020900@gmail.com>
References: <c10f8b$1q634q@ironport10.mayo.edu> <563CB04C.6020900@gmail.com>
Message-ID: <c10f8b$1q6iki@ironport10.mayo.edu>

Duncan,
   That's helpful.  Two follow-up questions:
1. Where would I have found this information?  I had looked at eval and model.frame.
2. What stops the following code from falling down the same rabbit hole?  Shouldn't it 
find base::cos first?

    library(survival)
    cos <- lung
    coxph(Surv(time, status) ~ age, data=cos)

Terry T.


On 11/06/2015 07:51 AM, Duncan Murdoch wrote:
> On 06/11/2015 7:36 AM, Therneau, Terry M., Ph.D. wrote:
>> I am currently puzzled by a seach path behavior.  I have a library of a dozen routines
>> getlabs(), getssn(), getecg(), ... that interface to local repositories and pull back
>> patient information.  All have a the first 6 arguments in common, and immediately call a
>> second routine to do initial processing of these 6.  The functions "joe" and "fred" below
>> capture the relevant portion of them.
>>     My puzzle is this: the last test in the "test" file works fine if these routines are
>> sourced and executed at the command line, it fails if the routines are bundled up and
>> loaded as a library. That test is motivated by a user who called his data set "t", and
>> ended up with a match to base:::t instead of his data, resulting in a strange error
>> message out of model.frame  --- you can always count on the users!  (There are a few
>> hundred.)
>>      I'm attempting to be careful with envr and enclos arguments -- how does base end up
>> earlier in the search path?   Perhaps this is clearly stated in the docs and just not
>> clear to me?  A working solution to the dilemma is of course more than welcome.
>
> I haven't followed through all the details in fred(), but I can answer the last question.
> In package code, the search order is:
>
> - the package environment
> - the imports to the package (with base being an implicit import)
> - the global environment and the rest of the search list.
>
> In code sourced to the global environment, only the third of these is searched.  Since
> base is in the second one, it is found first in the package version.
>
> Duncan Murdoch



[Rd] Puzzled by eval
Therneau, Terry M., Ph.D.