[Rd] package NAMESPACE question

Therneau, Terry M., Ph.D. therneau at mayo.edu
Wed Jan 29 15:20:10 CET 2014

An example of this is found in the coxph function in the survival library.  Look at it for 
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:
>> Hi,
>> >
>> >I've tried to put together a simpler example where I'm having the issue.
>> >
>> >I've built a foo package by only including a single .R file with the two
>> >functions listed below: trt and cmt. The second function calls the
>> >first. In the namespace file, if I only export(cmt), I get the following
>> >error message when running this
>> >
>> >library(foo)
>> >set.seed(1)
>> >dd <- data.frame(y = rbinom(100, 1, 0.5), treat = rbinom(100, 1, 0.5), x
>> >= rnorm(100),
>> >f = gl(4, 250, labels = c("A", "B", "C", "D")))
>> >dd2 <- cmt(y ~ x + f + trt(treat), data =dd)
>> >  > Error could not find function "trt"
>> >
>> >The problem is solved by doing export(cmt, trt) in the namespace.
>> >However, I'd like to avoid exporting trt and should not be required.
>> >Sorry I can't seem to figure this out by myself, and so I'd appreciate
>> >your help.
> You are asking for non-standard evaluation of the formula argument.  You
> want some parts of it to be evaluated in the global environment (f),
> some parts in the dd dataframe (x), and some parts evaluated in the
> package namespace (trt).  R is flexible so this is possible, but it's
> not the way that the terms function works, so you'll need to do more
> work yourself, including specifying what the evaluation rules should be
> in case a variable occurs in more than one of those locations.
> Duncan Murdoch
>> >

More information about the R-devel mailing list