Re: [Rd] package NAMESPACE question

2014-01-29 Thread Therneau, Terry M., Ph.D.
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-requ...@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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] package NAMESPACE question

2014-01-29 Thread Duncan Murdoch

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


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] package NAMESPACE question

2014-01-29 Thread Therneau, Terry M., Ph.D.
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:

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".


__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel