Skip to content

package NAMESPACE question

3 messages · Duncan Murdoch, Terry Therneau

#
An example of this is found in the coxph function in the survival library.  Look at it for 
guideance.
Within a coxph formula the expression "tt(x)" means to use a time transform on variable x. 
  The tt function is a fake, defined as tt <- function(x) x, whose only purpose is to 
allow the user to mark a variable for special treatment while staying inside the syntax 
rules of formulas.
   tt is not exported, because no one would ever want to use it.  It is documented within 
the coxph manual pages.  I did export it at one time, for exactly the reasons and error 
message detailed by Axel.  I recieved a request to cease doing so because of package 
issues --- someone had a package with a real and useful tt function, which would be masked 
if "require(survival)" ever occured, placing my library with its worthless function before 
his on the search path.  Enough things depend on the survival package that this was making 
his package's results unpredictable.

   The root issue is that formulas are often counterintuitive.  R treats them as though 
they were a function, but with a special evaluation context.  Thus even though a formula 
is evaluated within the coxph function, components of the formula are not "in" the 
survival namespace and so can't see non-exported functions in that namespace.   If you 
found this confusing be assured that you are not alone.

Terry Therneau
On 01/29/2014 05:00 AM, r-devel-request at r-project.org wrote:
#
On 29/01/2014 9:20 AM, Therneau, Terry M., Ph.D. wrote:
I'm not familiar with how coxph handles formulas.  Can a user put f(x) 
into a formula, where f is a user-defined function?  If a user defines a 
tt function, will coxph use theirs, or yours?

I'd say the answers for situations like this should be "yes" and 
"theirs", but that's not true of my tables package, where answers to the 
corresponding questions would be "yes" and "mine".

Duncan Murdoch
#
For the special tt mark in a formula, the answer is "mine".  Other than that coxph handles 
formulas in a completely standard way.

I don't see any other solution, frankly, unless I want to get really complex.  If formulas 
were not handled in an odd way, I'd simply add tt() to my namespace, not export it, and 
coxph would get my function by preference.  I had to engage in a little more trickery to 
get this behavior, but that is somewhat beside the point.

The key issue is actually the same one addressed by namespaces: when my function sees tt() 
in a formula, how does it decide whether to use its own function ("tt" is special, and 
documented as so), or believe that some other version is avaialable and should be used? 
If there is a tt() in the working directory?  In an attached library()?  In a directory on 
the search list (but ignore libraries)?  An attached "esp" library that reads the user's 
mind?

Terry T.
On 01/29/2014 09:48 AM, Duncan Murdoch wrote: