From murdoch.duncan at gmail.com  Wed Jan 29 16:48:15 2014
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Wed, 29 Jan 2014 10:48:15 -0500
Subject: [Rd] package NAMESPACE question
In-Reply-To: <d2fb82$886lse@ironport10.mayo.edu>
References: <mailman.19.1390993211.1898.r-devel@r-project.org>
	<d2fb82$886lse@ironport10.mayo.edu>
Message-ID: <52E922BF.1050805@gmail.com>

On 29/01/2014 9:20 AM, Therneau, Terry M., Ph.D. wrote:
> 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.

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
>
>     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
> >
> >> >
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] package NAMESPACE question
Duncan Murdoch