package NAMESPACE question

classic Classic list List threaded Threaded
26 messages Options
12
Reply | Threaded
Open this post in threaded view
|

package NAMESPACE question

Axel Urbiz
Hello,

I'm building a package. My code is stored in foo.R. This code has two
functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in export() under
the package NAMESPACE but NOT FUN2. After building the package when I call
FUN1 is giving me an error that cannot find FUN2.

I solved this by adding FUN2 in the export() NAMESPACE. However, what is
puzzling me is that I have other examples similar to the above (i.e., one
function calling another but only one exported) in the same package where I
don't get the error message.

Any idea of why that might be the case? My understanding is that export
only controls what is visible to the end user but functions not listed in
export() are still "usable" within the package.

In this case, the reason I'd like to avoid to export FUN2 is so I don't
have to add it in the package documentation.


Any guidance is much appreciated.

Regards,
Axel.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Dirk Eddelbuettel

On 25 January 2014 at 14:38, Axel Urbiz wrote:
| Hello,
|
| I'm building a package. My code is stored in foo.R. This code has two
| functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in export() under
| the package NAMESPACE but NOT FUN2. After building the package when I call
| FUN1 is giving me an error that cannot find FUN2.

Then you are doing something wrong in building, or possibly testing, the package.

"Everything within" can see everything else.

| I solved this by adding FUN2 in the export() NAMESPACE. However, what is
| puzzling me is that I have other examples similar to the above (i.e., one
| function calling another but only one exported) in the same package where I
| don't get the error message.
|
| Any idea of why that might be the case? My understanding is that export

We cannot tell without seeing the code.

I suggest you spend two minutes with package.skeleton(), create an empty
package, put something like these two functions in

         multiply <- function(x, k) x * k

         timestwo <- function(x) multiply(x, 2)

to convince yourself that timestwo() can in fact use multiply().

| only controls what is visible to the end user but functions not listed in
| export() are still "usable" within the package.
|
| In this case, the reason I'd like to avoid to export FUN2 is so I don't
| have to add it in the package documentation.
|
|
| Any guidance is much appreciated.
|
| Regards,
| Axel.
|
| [[alternative HTML version deleted]]
|
| ______________________________________________
| [hidden email] mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel

--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Uwe Ligges-3


On 25.01.2014 14:53, Dirk Eddelbuettel wrote:

>
> On 25 January 2014 at 14:38, Axel Urbiz wrote:
> | Hello,
> |
> | I'm building a package. My code is stored in foo.R. This code has two
> | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in export() under
> | the package NAMESPACE but NOT FUN2. After building the package when I call
> | FUN1 is giving me an error that cannot find FUN2.
>
> Then you are doing something wrong in building, or possibly testing, the package.


I guess you have FUN1 in your Workspace and using that rather than the
one in your package.

Uwe Ligges


> "Everything within" can see everything else.
>
> | I solved this by adding FUN2 in the export() NAMESPACE. However, what is
> | puzzling me is that I have other examples similar to the above (i.e., one
> | function calling another but only one exported) in the same package where I
> | don't get the error message.
> |
> | Any idea of why that might be the case? My understanding is that export
>
> We cannot tell without seeing the code.
>
> I suggest you spend two minutes with package.skeleton(), create an empty
> package, put something like these two functions in
>
>           multiply <- function(x, k) x * k
>
>           timestwo <- function(x) multiply(x, 2)
>
> to convince yourself that timestwo() can in fact use multiply().
>
> | only controls what is visible to the end user but functions not listed in
> | export() are still "usable" within the package.
> |
> | In this case, the reason I'd like to avoid to export FUN2 is so I don't
> | have to add it in the package documentation.
> |
> |
> | Any guidance is much appreciated.
> |
> | Regards,
> | Axel.
> |
> | [[alternative HTML version deleted]]
> |
> | ______________________________________________
> | [hidden email] mailing list
> | https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
Dirk and Uwe, many thanks both for your responses. I'm still having the
same issue. Here's in more detail:

As Dirk suggested, I've done the following:

* I've created a package named {foo}.

* {foo} only has one file named test.R which includes exactly the following
code:

multiply <- function(x,y) x * y

timestwo <- function(x) multiply(x, 2)

* I've modified the NAMESPACE to include ONLY the following line:

export(multiply)

* I've successfully built and installed the foo package

* Then in R I got this:

> library(foo)

> multiply(2,3)

[1] 6

> timestwo(2)

Error: could not find function "timestwo"

>

* However, if in the NAMESPACE I write instead export(multiply, timestwo),
then I don't get the error above.

* btw, here's my session info

> sessionInfo()

R Under development (unstable) (2014-01-17 r64821)

Platform: x86_64-apple-darwin10.8.0 (64-bit)

locale:

[1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8

attached base packages:

[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:

[1] foo_1.0


Thanks,

Axel.








On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <[hidden email]
> wrote:

>
>
> On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
>
>>
>> On 25 January 2014 at 14:38, Axel Urbiz wrote:
>> | Hello,
>> |
>> | I'm building a package. My code is stored in foo.R. This code has two
>> | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in export()
>> under
>> | the package NAMESPACE but NOT FUN2. After building the package when I
>> call
>> | FUN1 is giving me an error that cannot find FUN2.
>>
>> Then you are doing something wrong in building, or possibly testing, the
>> package.
>>
>
>
> I guess you have FUN1 in your Workspace and using that rather than the one
> in your package.
>
> Uwe Ligges
>
>
>
>  "Everything within" can see everything else.
>>
>> | I solved this by adding FUN2 in the export() NAMESPACE. However, what is
>> | puzzling me is that I have other examples similar to the above (i.e.,
>> one
>> | function calling another but only one exported) in the same package
>> where I
>> | don't get the error message.
>> |
>> | Any idea of why that might be the case? My understanding is that export
>>
>> We cannot tell without seeing the code.
>>
>> I suggest you spend two minutes with package.skeleton(), create an empty
>> package, put something like these two functions in
>>
>>           multiply <- function(x, k) x * k
>>
>>           timestwo <- function(x) multiply(x, 2)
>>
>> to convince yourself that timestwo() can in fact use multiply().
>>
>> | only controls what is visible to the end user but functions not listed
>> in
>> | export() are still "usable" within the package.
>> |
>> | In this case, the reason I'd like to avoid to export FUN2 is so I don't
>> | have to add it in the package documentation.
>> |
>> |
>> | Any guidance is much appreciated.
>> |
>> | Regards,
>> | Axel.
>> |
>> |       [[alternative HTML version deleted]]
>> |
>> | ______________________________________________
>> | [hidden email] mailing list
>> | https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Dirk Eddelbuettel

On 25 January 2014 at 20:26, Axel Urbiz wrote:
| Dirk and Uwe, many thanks both for your responses. I'm still having the same
| issue. Here's in more detail:
|
| As Dirk suggested, I've done the following:
|
| * I've created a package named {foo}. 
|
| * {foo} only has one file named test.R which includes exactly the following
| code:
|
|
| multiply <- function(x,y) x * y
|
| timestwo <- function(x) multiply(x, 2)
|
| * I've modified the NAMESPACE to include ONLY the following line:
|
| export(multiply)
|
| * I've successfully built and installed the foo package
|
| * Then in R I got this:
|
| > library(foo)
|
| > multiply(2,3)
|
| [1] 6
|
| > timestwo(2)
|
| Error: could not find function "timestwo"
|
| > 
|
| * However, if in the NAMESPACE I write instead export(multiply, timestwo), then
| I don't get the error above.

That. Works. As. Designed. And. Documented.

  Case 1)  You _export only multiply_. Hence timestwo is not found. Hint:
  learn about the difference between '::' and ':::' as well.

  Case 2)  You _export both_. Both are found.

No mystery.  What other effect do you expect export() to have?

Dirk

|
| * btw, here's my session info
|
| > sessionInfo()
|
| R Under development (unstable) (2014-01-17 r64821)
|
| Platform: x86_64-apple-darwin10.8.0 (64-bit)
|
| locale:
|
| [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
|
| attached base packages:
|
| [1] stats     graphics  grDevices utils     datasets  methods   base  
|   
|
| other attached packages:
|
| [1] foo_1.0
|
|
| Thanks,
|
| Axel.  
|
|
|
|
|
|
|
|
| On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <[hidden email]>
| wrote:
|
|
|
|     On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
|
|
|         On 25 January 2014 at 14:38, Axel Urbiz wrote:
|         | Hello,
|         |
|         | I'm building a package. My code is stored in foo.R. This code has two
|         | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in export()
|         under
|         | the package NAMESPACE but NOT FUN2. After building the package when I
|         call
|         | FUN1 is giving me an error that cannot find FUN2.
|
|         Then you are doing something wrong in building, or possibly testing,
|         the package.
|
|
|
|     I guess you have FUN1 in your Workspace and using that rather than the one
|     in your package.
|
|     Uwe Ligges
|
|
|
|
|         "Everything within" can see everything else.
|
|         | I solved this by adding FUN2 in the export() NAMESPACE. However, what
|         is
|         | puzzling me is that I have other examples similar to the above (i.e.,
|         one
|         | function calling another but only one exported) in the same package
|         where I
|         | don't get the error message.
|         |
|         | Any idea of why that might be the case? My understanding is that
|         export
|
|         We cannot tell without seeing the code.
|
|         I suggest you spend two minutes with package.skeleton(), create an
|         empty
|         package, put something like these two functions in
|
|                   multiply <- function(x, k) x * k
|
|                   timestwo <- function(x) multiply(x, 2)
|
|         to convince yourself that timestwo() can in fact use multiply().
|
|         | only controls what is visible to the end user but functions not
|         listed in
|         | export() are still "usable" within the package.
|         |
|         | In this case, the reason I'd like to avoid to export FUN2 is so I
|         don't
|         | have to add it in the package documentation.
|         |
|         |
|         | Any guidance is much appreciated.
|         |
|         | Regards,
|         | Axel.
|         |
|         |       [[alternative HTML version deleted]]
|         |
|         | ______________________________________________
|         | [hidden email] mailing list
|         | https://stat.ethz.ch/mailman/listinfo/r-devel
|
|
|

--
Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
Thanks Dirk. Sorry...It's my misunderstanding about "export" in the
NAMESPACE then. Essentially, as I understand it now, I should export all
functions that are used in the package? Why would I not export a certain
function?

In the example above, say I wanted to avoid documenting the function
"timestwo" in the foo package. Is there a way to do that? Otherwise in R
CMD check foo, I get this "Undocumented code objects: ‘timestwo’".

Thanks again for all your help!
Axel.


On Sat, Jan 25, 2014 at 8:32 PM, Dirk Eddelbuettel <[hidden email]> wrote:

>
> On 25 January 2014 at 20:26, Axel Urbiz wrote:
> | Dirk and Uwe, many thanks both for your responses. I'm still having the
> same
> | issue. Here's in more detail:
> |
> | As Dirk suggested, I've done the following:
> |
> | * I've created a package named {foo}.
> |
> | * {foo} only has one file named test.R which includes exactly the
> following
> | code:
> |
> |
> | multiply <- function(x,y) x * y
> |
> | timestwo <- function(x) multiply(x, 2)
> |
> | * I've modified the NAMESPACE to include ONLY the following line:
> |
> | export(multiply)
> |
> | * I've successfully built and installed the foo package
> |
> | * Then in R I got this:
> |
> | > library(foo)
> |
> | > multiply(2,3)
> |
> | [1] 6
> |
> | > timestwo(2)
> |
> | Error: could not find function "timestwo"
> |
> | >
> |
> | * However, if in the NAMESPACE I write instead export(multiply,
> timestwo), then
> | I don't get the error above.
>
> That. Works. As. Designed. And. Documented.
>
>   Case 1)  You _export only multiply_. Hence timestwo is not found. Hint:
>   learn about the difference between '::' and ':::' as well.
>
>   Case 2)  You _export both_. Both are found.
>
> No mystery.  What other effect do you expect export() to have?
>
> Dirk
>
> |
> | * btw, here's my session info
> |
> | > sessionInfo()
> |
> | R Under development (unstable) (2014-01-17 r64821)
> |
> | Platform: x86_64-apple-darwin10.8.0 (64-bit)
> |
> | locale:
> |
> | [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
> |
> | attached base packages:
> |
> | [1] stats     graphics  grDevices utils     datasets  methods   base
> |
> |
> | other attached packages:
> |
> | [1] foo_1.0
> |
> |
> | Thanks,
> |
> | Axel.
> |
> |
> |
> |
> |
> |
> |
> |
> | On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <
> [hidden email]>
> | wrote:
> |
> |
> |
> |     On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
> |
> |
> |         On 25 January 2014 at 14:38, Axel Urbiz wrote:
> |         | Hello,
> |         |
> |         | I'm building a package. My code is stored in foo.R. This code
> has two
> |         | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in
> export()
> |         under
> |         | the package NAMESPACE but NOT FUN2. After building the package
> when I
> |         call
> |         | FUN1 is giving me an error that cannot find FUN2.
> |
> |         Then you are doing something wrong in building, or possibly
> testing,
> |         the package.
> |
> |
> |
> |     I guess you have FUN1 in your Workspace and using that rather than
> the one
> |     in your package.
> |
> |     Uwe Ligges
> |
> |
> |
> |
> |         "Everything within" can see everything else.
> |
> |         | I solved this by adding FUN2 in the export() NAMESPACE.
> However, what
> |         is
> |         | puzzling me is that I have other examples similar to the above
> (i.e.,
> |         one
> |         | function calling another but only one exported) in the same
> package
> |         where I
> |         | don't get the error message.
> |         |
> |         | Any idea of why that might be the case? My understanding is
> that
> |         export
> |
> |         We cannot tell without seeing the code.
> |
> |         I suggest you spend two minutes with package.skeleton(), create
> an
> |         empty
> |         package, put something like these two functions in
> |
> |                   multiply <- function(x, k) x * k
> |
> |                   timestwo <- function(x) multiply(x, 2)
> |
> |         to convince yourself that timestwo() can in fact use multiply().
> |
> |         | only controls what is visible to the end user but functions not
> |         listed in
> |         | export() are still "usable" within the package.
> |         |
> |         | In this case, the reason I'd like to avoid to export FUN2 is
> so I
> |         don't
> |         | have to add it in the package documentation.
> |         |
> |         |
> |         | Any guidance is much appreciated.
> |         |
> |         | Regards,
> |         | Axel.
> |         |
> |         |       [[alternative HTML version deleted]]
> |         |
> |         | ______________________________________________
> |         | [hidden email] mailing list
> |         | https://stat.ethz.ch/mailman/listinfo/r-devel
> |
> |
> |
>
> --
> Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Gabriel Becker
Axel,

R CMD check and CRAN's submission guidelines do not allow undocumented
*user facing* (i.e. exported) functions or objects in packages.

If you want the users of your package to be able calling the timestwo
function directly, it needs to be documented (and exported).

If, on the other hand, the timestwo function is only used internally by
other functions in your package, and there is no reason for the end user to
be calling it, it does not need to be exported or documented. The flip side
of this is that, as you saw, non-exported objects in packages are not
visible to the user when your package is loaded (::: not withstanding, but
there should "virtually never" be a situation where users using your
package normally should  need to be using :::).

You can't have an undocumented function in your package which users can
call directly from the commandline simply by typing its name at the prompt.
Or rather, you absolutely can, but CRAN will reject the package.

HTH,
~G


On Sat, Jan 25, 2014 at 11:47 AM, Axel Urbiz <[hidden email]> wrote:

> Thanks Dirk. Sorry...It's my misunderstanding about "export" in the
> NAMESPACE then. Essentially, as I understand it now, I should export all
> functions that are used in the package? Why would I not export a certain
> function?
>
> In the example above, say I wanted to avoid documenting the function
> "timestwo" in the foo package. Is there a way to do that? Otherwise in R
> CMD check foo, I get this "Undocumented code objects: ‘timestwo’".
>
> Thanks again for all your help!
> Axel.
>
>
> On Sat, Jan 25, 2014 at 8:32 PM, Dirk Eddelbuettel <[hidden email]> wrote:
>
> >
> > On 25 January 2014 at 20:26, Axel Urbiz wrote:
> > | Dirk and Uwe, many thanks both for your responses. I'm still having the
> > same
> > | issue. Here's in more detail:
> > |
> > | As Dirk suggested, I've done the following:
> > |
> > | * I've created a package named {foo}.
> > |
> > | * {foo} only has one file named test.R which includes exactly the
> > following
> > | code:
> > |
> > |
> > | multiply <- function(x,y) x * y
> > |
> > | timestwo <- function(x) multiply(x, 2)
> > |
> > | * I've modified the NAMESPACE to include ONLY the following line:
> > |
> > | export(multiply)
> > |
> > | * I've successfully built and installed the foo package
> > |
> > | * Then in R I got this:
> > |
> > | > library(foo)
> > |
> > | > multiply(2,3)
> > |
> > | [1] 6
> > |
> > | > timestwo(2)
> > |
> > | Error: could not find function "timestwo"
> > |
> > | >
> > |
> > | * However, if in the NAMESPACE I write instead export(multiply,
> > timestwo), then
> > | I don't get the error above.
> >
> > That. Works. As. Designed. And. Documented.
> >
> >   Case 1)  You _export only multiply_. Hence timestwo is not found. Hint:
> >   learn about the difference between '::' and ':::' as well.
> >
> >   Case 2)  You _export both_. Both are found.
> >
> > No mystery.  What other effect do you expect export() to have?
> >
> > Dirk
> >
> > |
> > | * btw, here's my session info
> > |
> > | > sessionInfo()
> > |
> > | R Under development (unstable) (2014-01-17 r64821)
> > |
> > | Platform: x86_64-apple-darwin10.8.0 (64-bit)
> > |
> > | locale:
> > |
> > | [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
> > |
> > | attached base packages:
> > |
> > | [1] stats     graphics  grDevices utils     datasets  methods   base
> > |
> > |
> > | other attached packages:
> > |
> > | [1] foo_1.0
> > |
> > |
> > | Thanks,
> > |
> > | Axel.
> > |
> > |
> > |
> > |
> > |
> > |
> > |
> > |
> > | On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <
> > [hidden email]>
> > | wrote:
> > |
> > |
> > |
> > |     On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
> > |
> > |
> > |         On 25 January 2014 at 14:38, Axel Urbiz wrote:
> > |         | Hello,
> > |         |
> > |         | I'm building a package. My code is stored in foo.R. This code
> > has two
> > |         | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in
> > export()
> > |         under
> > |         | the package NAMESPACE but NOT FUN2. After building the
> package
> > when I
> > |         call
> > |         | FUN1 is giving me an error that cannot find FUN2.
> > |
> > |         Then you are doing something wrong in building, or possibly
> > testing,
> > |         the package.
> > |
> > |
> > |
> > |     I guess you have FUN1 in your Workspace and using that rather than
> > the one
> > |     in your package.
> > |
> > |     Uwe Ligges
> > |
> > |
> > |
> > |
> > |         "Everything within" can see everything else.
> > |
> > |         | I solved this by adding FUN2 in the export() NAMESPACE.
> > However, what
> > |         is
> > |         | puzzling me is that I have other examples similar to the
> above
> > (i.e.,
> > |         one
> > |         | function calling another but only one exported) in the same
> > package
> > |         where I
> > |         | don't get the error message.
> > |         |
> > |         | Any idea of why that might be the case? My understanding is
> > that
> > |         export
> > |
> > |         We cannot tell without seeing the code.
> > |
> > |         I suggest you spend two minutes with package.skeleton(), create
> > an
> > |         empty
> > |         package, put something like these two functions in
> > |
> > |                   multiply <- function(x, k) x * k
> > |
> > |                   timestwo <- function(x) multiply(x, 2)
> > |
> > |         to convince yourself that timestwo() can in fact use
> multiply().
> > |
> > |         | only controls what is visible to the end user but functions
> not
> > |         listed in
> > |         | export() are still "usable" within the package.
> > |         |
> > |         | In this case, the reason I'd like to avoid to export FUN2 is
> > so I
> > |         don't
> > |         | have to add it in the package documentation.
> > |         |
> > |         |
> > |         | Any guidance is much appreciated.
> > |         |
> > |         | Regards,
> > |         | Axel.
> > |         |
> > |         |       [[alternative HTML version deleted]]
> > |         |
> > |         | ______________________________________________
> > |         | [hidden email] mailing list
> > |         | https://stat.ethz.ch/mailman/listinfo/r-devel
> > |
> > |
> > |
> >
> > --
> > Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
> >
>
>         [[alternative HTML version deleted]]
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

--
Gabriel Becker
Graduate Student
Statistics Department
University of California, Davis

        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
Thanks Gabriel. Your explanation is very clear.

Seeing the example above, I made a mistake. If I export(timestwo) (and not
multiply), then calling timestwo in the package works fine.

However, in my real package I have a situation like the above:

contr.none <- function(n, contrasts) {

  if (length(n) == 1)

    contr.treatment(n, contrasts = n<=2)

  else

    contr.treatment(n, contrasts = length(unique(n))<=2)

}


The contr.none is called by another function named myfun(). myfun is
exported but not contr.none.

Then, when I try to use myfun() it complains as follows:

Error in get(ctr, mode = "function", envir = parent.frame()) :

  object 'contr.none' of mode 'function' was not found


Thanks again,

Axel.




On Sat, Jan 25, 2014 at 9:37 PM, Gabriel Becker <[hidden email]>wrote:

> Axel,
>
> R CMD check and CRAN's submission guidelines do not allow undocumented
> *user facing* (i.e. exported) functions or objects in packages.
>
> If you want the users of your package to be able calling the timestwo
> function directly, it needs to be documented (and exported).
>
> If, on the other hand, the timestwo function is only used internally by
> other functions in your package, and there is no reason for the end user to
> be calling it, it does not need to be exported or documented. The flip side
> of this is that, as you saw, non-exported objects in packages are not
> visible to the user when your package is loaded (::: not withstanding, but
> there should "virtually never" be a situation where users using your
> package normally should  need to be using :::).
>
> You can't have an undocumented function in your package which users can
> call directly from the commandline simply by typing its name at the prompt.
> Or rather, you absolutely can, but CRAN will reject the package.
>
> HTH,
> ~G
>
>
> On Sat, Jan 25, 2014 at 11:47 AM, Axel Urbiz <[hidden email]> wrote:
>
>> Thanks Dirk. Sorry...It's my misunderstanding about "export" in the
>> NAMESPACE then. Essentially, as I understand it now, I should export all
>> functions that are used in the package? Why would I not export a certain
>> function?
>>
>> In the example above, say I wanted to avoid documenting the function
>> "timestwo" in the foo package. Is there a way to do that? Otherwise in R
>> CMD check foo, I get this "Undocumented code objects: ‘timestwo’".
>>
>> Thanks again for all your help!
>> Axel.
>>
>>
>> On Sat, Jan 25, 2014 at 8:32 PM, Dirk Eddelbuettel <[hidden email]>
>> wrote:
>>
>> >
>> > On 25 January 2014 at 20:26, Axel Urbiz wrote:
>> > | Dirk and Uwe, many thanks both for your responses. I'm still having
>> the
>> > same
>> > | issue. Here's in more detail:
>> > |
>> > | As Dirk suggested, I've done the following:
>> > |
>> > | * I've created a package named {foo}.
>> > |
>> > | * {foo} only has one file named test.R which includes exactly the
>> > following
>> > | code:
>> > |
>> > |
>> > | multiply <- function(x,y) x * y
>> > |
>> > | timestwo <- function(x) multiply(x, 2)
>> > |
>> > | * I've modified the NAMESPACE to include ONLY the following line:
>> > |
>> > | export(multiply)
>> > |
>> > | * I've successfully built and installed the foo package
>> > |
>> > | * Then in R I got this:
>> > |
>> > | > library(foo)
>> > |
>> > | > multiply(2,3)
>> > |
>> > | [1] 6
>> > |
>> > | > timestwo(2)
>> > |
>> > | Error: could not find function "timestwo"
>> > |
>> > | >
>> > |
>> > | * However, if in the NAMESPACE I write instead export(multiply,
>> > timestwo), then
>> > | I don't get the error above.
>> >
>> > That. Works. As. Designed. And. Documented.
>> >
>> >   Case 1)  You _export only multiply_. Hence timestwo is not found.
>> Hint:
>> >   learn about the difference between '::' and ':::' as well.
>> >
>> >   Case 2)  You _export both_. Both are found.
>> >
>> > No mystery.  What other effect do you expect export() to have?
>> >
>> > Dirk
>> >
>> > |
>> > | * btw, here's my session info
>> > |
>> > | > sessionInfo()
>> > |
>> > | R Under development (unstable) (2014-01-17 r64821)
>> > |
>> > | Platform: x86_64-apple-darwin10.8.0 (64-bit)
>> > |
>> > | locale:
>> > |
>> > | [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
>> > |
>> > | attached base packages:
>> > |
>> > | [1] stats     graphics  grDevices utils     datasets  methods   base
>> > |
>> > |
>> > | other attached packages:
>> > |
>> > | [1] foo_1.0
>> > |
>> > |
>> > | Thanks,
>> > |
>> > | Axel.
>> > |
>> > |
>> > |
>> > |
>> > |
>> > |
>> > |
>> > |
>> > | On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <
>> > [hidden email]>
>> > | wrote:
>> > |
>> > |
>> > |
>> > |     On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
>> > |
>> > |
>> > |         On 25 January 2014 at 14:38, Axel Urbiz wrote:
>> > |         | Hello,
>> > |         |
>> > |         | I'm building a package. My code is stored in foo.R. This
>> code
>> > has two
>> > |         | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in
>> > export()
>> > |         under
>> > |         | the package NAMESPACE but NOT FUN2. After building the
>> package
>> > when I
>> > |         call
>> > |         | FUN1 is giving me an error that cannot find FUN2.
>> > |
>> > |         Then you are doing something wrong in building, or possibly
>> > testing,
>> > |         the package.
>> > |
>> > |
>> > |
>> > |     I guess you have FUN1 in your Workspace and using that rather than
>> > the one
>> > |     in your package.
>> > |
>> > |     Uwe Ligges
>> > |
>> > |
>> > |
>> > |
>> > |         "Everything within" can see everything else.
>> > |
>> > |         | I solved this by adding FUN2 in the export() NAMESPACE.
>> > However, what
>> > |         is
>> > |         | puzzling me is that I have other examples similar to the
>> above
>> > (i.e.,
>> > |         one
>> > |         | function calling another but only one exported) in the same
>> > package
>> > |         where I
>> > |         | don't get the error message.
>> > |         |
>> > |         | Any idea of why that might be the case? My understanding is
>> > that
>> > |         export
>> > |
>> > |         We cannot tell without seeing the code.
>> > |
>> > |         I suggest you spend two minutes with package.skeleton(),
>> create
>> > an
>> > |         empty
>> > |         package, put something like these two functions in
>> > |
>> > |                   multiply <- function(x, k) x * k
>> > |
>> > |                   timestwo <- function(x) multiply(x, 2)
>> > |
>> > |         to convince yourself that timestwo() can in fact use
>> multiply().
>> > |
>> > |         | only controls what is visible to the end user but functions
>> not
>> > |         listed in
>> > |         | export() are still "usable" within the package.
>> > |         |
>> > |         | In this case, the reason I'd like to avoid to export FUN2 is
>> > so I
>> > |         don't
>> > |         | have to add it in the package documentation.
>> > |         |
>> > |         |
>> > |         | Any guidance is much appreciated.
>> > |         |
>> > |         | Regards,
>> > |         | Axel.
>> > |         |
>> > |         |       [[alternative HTML version deleted]]
>> > |         |
>> > |         | ______________________________________________
>> > |         | [hidden email] mailing list
>> > |         | https://stat.ethz.ch/mailman/listinfo/r-devel
>> > |
>> > |
>> > |
>> >
>> > --
>> > Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
>> >
>>
>>         [[alternative HTML version deleted]]
>>
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>
>
> --
> Gabriel Becker
> Graduate Student
> Statistics Department
> University of California, Davis
>
        [[alternative HTML version deleted]]


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Gabriel Becker
I haven't seen all of your code, but are you using get to obtain the object
yourself? If so, that is likely your problem, as that is (roughly)
equivalent to typing the name at the prompt, it won't see unexported
functions. It shouldn't be necessary to do, though.

contrasts = switch(ctr,
"contr.none" = contr.none(<stuff>),
"contr.some" = contr.some(<stuff>),
default = stop("unrecognized contrast function")
)

or something similar is much more legible, and thus maintainable, anyway.

Non-standard lookup/evaluation can be powerful, and even necessary to do
certain particular things, but it's a very large and highly specialized
hammer that is usually more trouble than it is worth. Many things people
are tempted to do with it don't really require it at all, and in fact are
easier without it.

HTH,
~G


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Duncan Murdoch-2
In reply to this post by Axel Urbiz
On 14-01-25 2:47 PM, Axel Urbiz wrote:
> Thanks Dirk. Sorry...It's my misunderstanding about "export" in the
> NAMESPACE then. Essentially, as I understand it now, I should export all
> functions that are used in the package? Why would I not export a certain
> function?
>
> In the example above, say I wanted to avoid documenting the function
> "timestwo" in the foo package. Is there a way to do that? Otherwise in R
> CMD check foo, I get this "Undocumented code objects: ‘timestwo’".


I think you missed the point of Dirk's example.  In his example,
timestwo was the function that you *did* want to export and document.
multiply was the function you wanted to keep local.

So you should export timestwo, and not multiply.  You will then need to
document timestwo, but you won't need to document multiply.

That will work.

Duncan Murdoch

>
> Thanks again for all your help!
> Axel.
>
>
> On Sat, Jan 25, 2014 at 8:32 PM, Dirk Eddelbuettel <[hidden email]> wrote:
>
>>
>> On 25 January 2014 at 20:26, Axel Urbiz wrote:
>> | Dirk and Uwe, many thanks both for your responses. I'm still having the
>> same
>> | issue. Here's in more detail:
>> |
>> | As Dirk suggested, I've done the following:
>> |
>> | * I've created a package named {foo}.
>> |
>> | * {foo} only has one file named test.R which includes exactly the
>> following
>> | code:
>> |
>> |
>> | multiply <- function(x,y) x * y
>> |
>> | timestwo <- function(x) multiply(x, 2)
>> |
>> | * I've modified the NAMESPACE to include ONLY the following line:
>> |
>> | export(multiply)
>> |
>> | * I've successfully built and installed the foo package
>> |
>> | * Then in R I got this:
>> |
>> | > library(foo)
>> |
>> | > multiply(2,3)
>> |
>> | [1] 6
>> |
>> | > timestwo(2)
>> |
>> | Error: could not find function "timestwo"
>> |
>> | >
>> |
>> | * However, if in the NAMESPACE I write instead export(multiply,
>> timestwo), then
>> | I don't get the error above.
>>
>> That. Works. As. Designed. And. Documented.
>>
>>    Case 1)  You _export only multiply_. Hence timestwo is not found. Hint:
>>    learn about the difference between '::' and ':::' as well.
>>
>>    Case 2)  You _export both_. Both are found.
>>
>> No mystery.  What other effect do you expect export() to have?
>>
>> Dirk
>>
>> |
>> | * btw, here's my session info
>> |
>> | > sessionInfo()
>> |
>> | R Under development (unstable) (2014-01-17 r64821)
>> |
>> | Platform: x86_64-apple-darwin10.8.0 (64-bit)
>> |
>> | locale:
>> |
>> | [1] en_CA.UTF-8/en_CA.UTF-8/en_CA.UTF-8/C/en_CA.UTF-8/en_CA.UTF-8
>> |
>> | attached base packages:
>> |
>> | [1] stats     graphics  grDevices utils     datasets  methods   base
>> |
>> |
>> | other attached packages:
>> |
>> | [1] foo_1.0
>> |
>> |
>> | Thanks,
>> |
>> | Axel.
>> |
>> |
>> |
>> |
>> |
>> |
>> |
>> |
>> | On Sat, Jan 25, 2014 at 4:32 PM, Uwe Ligges <
>> [hidden email]>
>> | wrote:
>> |
>> |
>> |
>> |     On 25.01.2014 14:53, Dirk Eddelbuettel wrote:
>> |
>> |
>> |         On 25 January 2014 at 14:38, Axel Urbiz wrote:
>> |         | Hello,
>> |         |
>> |         | I'm building a package. My code is stored in foo.R. This code
>> has two
>> |         | functions FUN1 and FUN2. FUN1 calls FUN2. FUN1 is listed in
>> export()
>> |         under
>> |         | the package NAMESPACE but NOT FUN2. After building the package
>> when I
>> |         call
>> |         | FUN1 is giving me an error that cannot find FUN2.
>> |
>> |         Then you are doing something wrong in building, or possibly
>> testing,
>> |         the package.
>> |
>> |
>> |
>> |     I guess you have FUN1 in your Workspace and using that rather than
>> the one
>> |     in your package.
>> |
>> |     Uwe Ligges
>> |
>> |
>> |
>> |
>> |         "Everything within" can see everything else.
>> |
>> |         | I solved this by adding FUN2 in the export() NAMESPACE.
>> However, what
>> |         is
>> |         | puzzling me is that I have other examples similar to the above
>> (i.e.,
>> |         one
>> |         | function calling another but only one exported) in the same
>> package
>> |         where I
>> |         | don't get the error message.
>> |         |
>> |         | Any idea of why that might be the case? My understanding is
>> that
>> |         export
>> |
>> |         We cannot tell without seeing the code.
>> |
>> |         I suggest you spend two minutes with package.skeleton(), create
>> an
>> |         empty
>> |         package, put something like these two functions in
>> |
>> |                   multiply <- function(x, k) x * k
>> |
>> |                   timestwo <- function(x) multiply(x, 2)
>> |
>> |         to convince yourself that timestwo() can in fact use multiply().
>> |
>> |         | only controls what is visible to the end user but functions not
>> |         listed in
>> |         | export() are still "usable" within the package.
>> |         |
>> |         | In this case, the reason I'd like to avoid to export FUN2 is
>> so I
>> |         don't
>> |         | have to add it in the package documentation.
>> |         |
>> |         |
>> |         | Any guidance is much appreciated.
>> |         |
>> |         | Regards,
>> |         | Axel.
>> |         |
>> |         |       [[alternative HTML version deleted]]
>> |         |
>> |         | ______________________________________________
>> |         | [hidden email] mailing list
>> |         | https://stat.ethz.ch/mailman/listinfo/r-devel
>> |
>> |
>> |
>>
>> --
>> Dirk Eddelbuettel | [hidden email] | http://dirk.eddelbuettel.com
>>
>
> [[alternative HTML version deleted]]
>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
In reply to this post by Gabriel Becker
Thanks again all. Essentially, this is the section of the code that is
causing trouble. This is part of the (exported) function which calls
contr.none (not exported). As mentioned, when I call the exported function
it complains with the error described before.


  oldcontrasts <- unlist(options("contrasts"))
    if (cts)
        options(contrasts = c(unordered = "contr.none", ordered =
"contr.diff"))
    x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
    options(contrasts = oldcontrasts)

Axel.


On Sat, Jan 25, 2014 at 10:19 PM, Gabriel Becker <[hidden email]>wrote:

> I haven't seen all of your code, but are you using get to obtain the
> object yourself? If so, that is likely your problem, as that is (roughly)
> equivalent to typing the name at the prompt, it won't see unexported
> functions. It shouldn't be necessary to do, though.
>
> contrasts = switch(ctr,
> "contr.none" = contr.none(<stuff>),
> "contr.some" = contr.some(<stuff>),
> default = stop("unrecognized contrast function")
> )
>
> or something similar is much more legible, and thus maintainable, anyway.
>
> Non-standard lookup/evaluation can be powerful, and even necessary to do
> certain particular things, but it's a very large and highly specialized
> hammer that is usually more trouble than it is worth. Many things people
> are tempted to do with it don't really require it at all, and in fact are
> easier without it.
>
> HTH,
> ~G
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Duncan Murdoch-2
On 14-01-25 6:05 PM, Axel Urbiz wrote:

> Thanks again all. Essentially, this is the section of the code that is
> causing trouble. This is part of the (exported) function which calls
> contr.none (not exported). As mentioned, when I call the exported function
> it complains with the error described before.
>
>
>    oldcontrasts <- unlist(options("contrasts"))
>      if (cts)
>          options(contrasts = c(unordered = "contr.none", ordered =
> "contr.diff"))
>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
>      options(contrasts = oldcontrasts)

This is hugely incomplete.  Please stop wasting everyone's time, and
post something reproducible.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
Hi Duncan,

My most sincere apologies. It's really not my intention to waste anyones
time. More the opposite...for some reason I thought that the problem had to
do with my call to options() and thought that would be enough. Here's
something reproducible:

I built a foo package based on the code under the "----" below. In the
namespace file, I've only exported: trt and cmt (not contr.none and
contr.diff). Notice that cmt calls contr.none and contr.diff by default.

Then in R, I run this code and I get this error message:

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 in get(ctr, mode = "function", envir = parent.frame()) :
   object 'contr.none' of mode 'function' was not found

Thanks,
Axel.

--------------------------------------------

trt <- function(x) x

cmt <- function(formula, data, subset, na.action = na.pass, cts = TRUE)  {

  if (!inherits(formula, "formula"))
    stop("Method is only for formula objects.")
  mf <- match.call(expand.dots = FALSE)
  args <- match(c("formula", "data", "subset", "na.action"),
                names(mf), 0)
  mf <- mf[c(1, args)]
  mf$drop.unused.levels <- TRUE
  mf[[1]] <- as.name("model.frame")
  special <- "trt"
  mt <- if(missing(data)) terms(formula, special) else terms(formula,
special, data = data)
  mf$formula <- mt
  mf <- eval.parent(mf)
  Terms <- attr(mf, "terms")
  attr(Terms, "intercept") <- 0
  trt.var <- attr(Terms, "specials")$trt
  ct <- mf[, trt.var]
  y <- model.response(mf, "numeric")
  var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
  treat.names <- levels(as.factor(ct))
  oldcontrasts <- unlist(options("contrasts"))
  if (cts)
    options(contrasts = c(unordered = "contr.none", ordered = "contr.diff"))
  x <- model.matrix(terms(reformulate(var_names)),
                    mf, contrasts)
  options(contrasts = oldcontrasts)
  intercept <- which(colnames(x) == "(Intercept)")
  if (length(intercept > 0)) x <- x[, -intercept]
  return(x)
  }

#######################################
# An alternative contrasts function for unordered factors
# Ensures symmetric treatment of all levels of a factor
#######################################
contr.none <- function(n, contrasts) {
  if (length(n) == 1)
    contr.treatment(n, contrasts = n<=2)
  else
    contr.treatment(n, contrasts = length(unique(n))<=2)
}

#######################################
# An alternative contrasts function for ordered factors
# Ensures use of a difference penalty for such factors
#######################################
contr.diff <- function (n, contrasts = TRUE)
{
  if (is.numeric(n) && length(n) == 1) {
    if (n > 1)
      levs <- 1:n
    else stop("not enough degrees of freedom to define contrasts")
  }
  else {
    levs <- n
    n <- length(n)
  }
  contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
  contr[outer(1:n,1:n, ">=")] <- 1
  if (n < 2)
    stop(gettextf("contrasts not defined for %d degrees of freedom",
                  n - 1), domain = NA)
  if (contrasts)
    contr <- contr[, -1, drop = FALSE]
  contr
}



On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch <[hidden email]>wrote:

> On 14-01-25 6:05 PM, Axel Urbiz wrote:
>
>> Thanks again all. Essentially, this is the section of the code that is
>> causing trouble. This is part of the (exported) function which calls
>> contr.none (not exported). As mentioned, when I call the exported function
>> it complains with the error described before.
>>
>>
>>    oldcontrasts <- unlist(options("contrasts"))
>>      if (cts)
>>          options(contrasts = c(unordered = "contr.none", ordered =
>> "contr.diff"))
>>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
>>      options(contrasts = oldcontrasts)
>>
>
> This is hugely incomplete.  Please stop wasting everyone's time, and post
> something reproducible.
>
> Duncan Murdoch
>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Duncan Murdoch-2
On 14-01-26 9:34 AM, Axel Urbiz wrote:> Hi Duncan,
 >
 > My most sincere apologies. It's really not my intention to waste anyones
 > time. More the opposite...for some reason I thought that the problem had
 > to do with my call to options() and thought that would be enough. Here's
 > something reproducible:
 >
 > I built a foo package based on the code under the "----" below. In the
 > namespace file, I've only exported: trt and cmt (not contr.none and
 > contr.diff). Notice that cmt calls contr.none and contr.diff by default.
 >
 > Then in R, I run this code and I get this error message:
 >
 > 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 in get(ctr, mode = "function", envir = parent.frame()) :
 >     object 'contr.none' of mode 'function' was not found

In your call to model.matrix in cmt, you pass "contrasts" without
defining it.  This looks like an error, though it wouldn't cause the
message you saw, it's likely it would cause some strange problem.

I haven't tried putting your code in a package to see what difference
that makes.  It's your job to make a reproducible example, not mine.

Duncan Murdoch


 >
 > Thanks,
 > Axel.
 >
 > --------------------------------------------
 >
 > trt <- function(x) x
 >
 > cmt <- function(formula, data, subset, na.action = na.pass, cts =
TRUE)  {
 >    if (!inherits(formula, "formula"))
 >      stop("Method is only for formula objects.")
 >    mf <- match.call(expand.dots = FALSE)
 >    args <- match(c("formula", "data", "subset", "na.action"),
 >                  names(mf), 0)
 >    mf <- mf[c(1, args)]
 >    mf$drop.unused.levels <- TRUE
 >    mf[[1]] <- as.name <http://as.name>("model.frame")
 >    special <- "trt"
 >    mt <- if(missing(data)) terms(formula, special) else terms(formula,
 > special, data = data)
 >    mf$formula <- mt
 >    mf <- eval.parent(mf)
 >    Terms <- attr(mf, "terms")
 >    attr(Terms, "intercept") <- 0
 >    trt.var <- attr(Terms, "specials")$trt
 >    ct <- mf[, trt.var]
 >    y <- model.response(mf, "numeric")
 >    var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
 >    treat.names <- levels(as.factor(ct))
 >    oldcontrasts <- unlist(options("contrasts"))
 >    if (cts)
 >      options(contrasts = c(unordered = "contr.none", ordered =
 > "contr.diff"))
 >    x <- model.matrix(terms(reformulate(var_names)),
 >                      mf, contrasts)
 >    options(contrasts = oldcontrasts)
 >    intercept <- which(colnames(x) == "(Intercept)")
 >    if (length(intercept > 0)) x <- x[, -intercept]
 >    return(x)
 >    }
 > #######################################
 > # An alternative contrasts function for unordered factors
 > # Ensures symmetric treatment of all levels of a factor
 > #######################################
 > contr.none <- function(n, contrasts) {
 >    if (length(n) == 1)
 >      contr.treatment(n, contrasts = n<=2)
 >    else
 >      contr.treatment(n, contrasts = length(unique(n))<=2)
 > }
 >
 > #######################################
 > # An alternative contrasts function for ordered factors
 > # Ensures use of a difference penalty for such factors
 > #######################################
 > contr.diff <- function (n, contrasts = TRUE)
 > {
 >    if (is.numeric(n) && length(n) == 1) {
 >      if (n > 1)
 >        levs <- 1:n
 >      else stop("not enough degrees of freedom to define contrasts")
 >    }
 >    else {
 >      levs <- n
 >      n <- length(n)
 >    }
 >    contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
 >    contr[outer(1:n,1:n, ">=")] <- 1
 >    if (n < 2)
 >      stop(gettextf("contrasts not defined for %d degrees of freedom",
 >                    n - 1), domain = NA)
 >    if (contrasts)
 >      contr <- contr[, -1, drop = FALSE]
 >    contr
 > }
 >
 >
 >
 > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch
 > <[hidden email] <mailto:[hidden email]>> wrote:
 >
 >     On 14-01-25 6:05 PM, Axel Urbiz wrote:
 >
 >         Thanks again all. Essentially, this is the section of the code
 >         that is
 >         causing trouble. This is part of the (exported) function
which calls
 >         contr.none (not exported). As mentioned, when I call the
 >         exported function
 >         it complains with the error described before.
 >
 >
 >             oldcontrasts <- unlist(options("contrasts"))
 >               if (cts)
 >                   options(contrasts = c(unordered = "contr.none",
ordered =
 >         "contr.diff"))
 >               x <- model.matrix(terms(__reformulate(var_names)), mf,
 >         contrasts)
 >               options(contrasts = oldcontrasts)
 >
 >
 >     This is hugely incomplete.  Please stop wasting everyone's time, and
 >     post something reproducible.
 >
 >     Duncan Murdoch
 >
 >

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Henrik Bengtsson-3
In reply to this post by Axel Urbiz
On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]> wrote:

> Hi Duncan,
>
> My most sincere apologies. It's really not my intention to waste anyones
> time. More the opposite...for some reason I thought that the problem had to
> do with my call to options() and thought that would be enough. Here's
> something reproducible:
>
> I built a foo package based on the code under the "----" below. In the
> namespace file, I've only exported: trt and cmt (not contr.none and
> contr.diff). Notice that cmt calls contr.none and contr.diff by default.

As a start, try to export everything, particularly 'contr.none' and
'contr.diff' and see if that works.  Just a guess, but worth trying
out.

My $.02

/Henrik

>
> Then in R, I run this code and I get this error message:
>
> 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 in get(ctr, mode = "function", envir = parent.frame()) :
>    object 'contr.none' of mode 'function' was not found
>
> Thanks,
> Axel.
>
> --------------------------------------------
>
> trt <- function(x) x
>
> cmt <- function(formula, data, subset, na.action = na.pass, cts = TRUE)  {
>
>   if (!inherits(formula, "formula"))
>     stop("Method is only for formula objects.")
>   mf <- match.call(expand.dots = FALSE)
>   args <- match(c("formula", "data", "subset", "na.action"),
>                 names(mf), 0)
>   mf <- mf[c(1, args)]
>   mf$drop.unused.levels <- TRUE
>   mf[[1]] <- as.name("model.frame")
>   special <- "trt"
>   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> special, data = data)
>   mf$formula <- mt
>   mf <- eval.parent(mf)
>   Terms <- attr(mf, "terms")
>   attr(Terms, "intercept") <- 0
>   trt.var <- attr(Terms, "specials")$trt
>   ct <- mf[, trt.var]
>   y <- model.response(mf, "numeric")
>   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>   treat.names <- levels(as.factor(ct))
>   oldcontrasts <- unlist(options("contrasts"))
>   if (cts)
>     options(contrasts = c(unordered = "contr.none", ordered = "contr.diff"))
>   x <- model.matrix(terms(reformulate(var_names)),
>                     mf, contrasts)
>   options(contrasts = oldcontrasts)
>   intercept <- which(colnames(x) == "(Intercept)")
>   if (length(intercept > 0)) x <- x[, -intercept]
>   return(x)
>   }
>
> #######################################
> # An alternative contrasts function for unordered factors
> # Ensures symmetric treatment of all levels of a factor
> #######################################
> contr.none <- function(n, contrasts) {
>   if (length(n) == 1)
>     contr.treatment(n, contrasts = n<=2)
>   else
>     contr.treatment(n, contrasts = length(unique(n))<=2)
> }
>
> #######################################
> # An alternative contrasts function for ordered factors
> # Ensures use of a difference penalty for such factors
> #######################################
> contr.diff <- function (n, contrasts = TRUE)
> {
>   if (is.numeric(n) && length(n) == 1) {
>     if (n > 1)
>       levs <- 1:n
>     else stop("not enough degrees of freedom to define contrasts")
>   }
>   else {
>     levs <- n
>     n <- length(n)
>   }
>   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
>   contr[outer(1:n,1:n, ">=")] <- 1
>   if (n < 2)
>     stop(gettextf("contrasts not defined for %d degrees of freedom",
>                   n - 1), domain = NA)
>   if (contrasts)
>     contr <- contr[, -1, drop = FALSE]
>   contr
> }
>
>
>
> On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch <[hidden email]>wrote:
>
>> On 14-01-25 6:05 PM, Axel Urbiz wrote:
>>
>>> Thanks again all. Essentially, this is the section of the code that is
>>> causing trouble. This is part of the (exported) function which calls
>>> contr.none (not exported). As mentioned, when I call the exported function
>>> it complains with the error described before.
>>>
>>>
>>>    oldcontrasts <- unlist(options("contrasts"))
>>>      if (cts)
>>>          options(contrasts = c(unordered = "contr.none", ordered =
>>> "contr.diff"))
>>>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
>>>      options(contrasts = oldcontrasts)
>>>
>>
>> This is hugely incomplete.  Please stop wasting everyone's time, and post
>> something reproducible.
>>
>> Duncan Murdoch
>>
>>
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
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.

Thanks,
Axel.

----

#mycodefiles <- c("cmt.R")
#package.skeleton(name = "foo", code_files = mycodefiles)
#promptPackage("foo")

#where cmt.R includes the code below:

trt <- function(x) x

cmt <- function(formula, data, subset, na.action = na.pass)  {

  if (!inherits(formula, "formula"))
    stop("Method is only for formula objects.")
  mf <- match.call(expand.dots = FALSE)
  args <- match(c("formula", "data", "subset", "na.action"),
                names(mf), 0)
  mf <- mf[c(1, args)]
  mf$drop.unused.levels <- TRUE
  mf[[1]] <- as.name("model.frame")
  special <- "trt"
  mt <- if(missing(data)) terms(formula, special) else terms(formula,
special, data = data)
  browser()
  mf$formula <- mt
  mf <- eval.parent(mf)
  Terms <- attr(mf, "terms")
  attr(Terms, "intercept") <- 0
  trt.var <- attr(Terms, "specials")$trt
  ct <- mf[, trt.var]
  y <- model.response(mf, "numeric")
  var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
  x <- model.matrix(terms(reformulate(var_names)),
                    mf, contrasts)
  intercept <- which(colnames(x) == "(Intercept)")
  if (length(intercept > 0)) x <- x[, -intercept]
  return(x)
  }




On Mon, Jan 27, 2014 at 2:42 AM, Henrik Bengtsson <[hidden email]>wrote:

> On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]> wrote:
> > Hi Duncan,
> >
> > My most sincere apologies. It's really not my intention to waste anyones
> > time. More the opposite...for some reason I thought that the problem had
> to
> > do with my call to options() and thought that would be enough. Here's
> > something reproducible:
> >
> > I built a foo package based on the code under the "----" below. In the
> > namespace file, I've only exported: trt and cmt (not contr.none and
> > contr.diff). Notice that cmt calls contr.none and contr.diff by default.
>
> As a start, try to export everything, particularly 'contr.none' and
> 'contr.diff' and see if that works.  Just a guess, but worth trying
> out.
>
> My $.02
>
> /Henrik
>
> >
> > Then in R, I run this code and I get this error message:
> >
> > 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 in get(ctr, mode = "function", envir = parent.frame()) :
> >    object 'contr.none' of mode 'function' was not found
> >
> > Thanks,
> > Axel.
> >
> > --------------------------------------------
> >
> > trt <- function(x) x
> >
> > cmt <- function(formula, data, subset, na.action = na.pass, cts = TRUE)
>  {
> >
> >   if (!inherits(formula, "formula"))
> >     stop("Method is only for formula objects.")
> >   mf <- match.call(expand.dots = FALSE)
> >   args <- match(c("formula", "data", "subset", "na.action"),
> >                 names(mf), 0)
> >   mf <- mf[c(1, args)]
> >   mf$drop.unused.levels <- TRUE
> >   mf[[1]] <- as.name("model.frame")
> >   special <- "trt"
> >   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> > special, data = data)
> >   mf$formula <- mt
> >   mf <- eval.parent(mf)
> >   Terms <- attr(mf, "terms")
> >   attr(Terms, "intercept") <- 0
> >   trt.var <- attr(Terms, "specials")$trt
> >   ct <- mf[, trt.var]
> >   y <- model.response(mf, "numeric")
> >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
> >   treat.names <- levels(as.factor(ct))
> >   oldcontrasts <- unlist(options("contrasts"))
> >   if (cts)
> >     options(contrasts = c(unordered = "contr.none", ordered =
> "contr.diff"))
> >   x <- model.matrix(terms(reformulate(var_names)),
> >                     mf, contrasts)
> >   options(contrasts = oldcontrasts)
> >   intercept <- which(colnames(x) == "(Intercept)")
> >   if (length(intercept > 0)) x <- x[, -intercept]
> >   return(x)
> >   }
> >
> > #######################################
> > # An alternative contrasts function for unordered factors
> > # Ensures symmetric treatment of all levels of a factor
> > #######################################
> > contr.none <- function(n, contrasts) {
> >   if (length(n) == 1)
> >     contr.treatment(n, contrasts = n<=2)
> >   else
> >     contr.treatment(n, contrasts = length(unique(n))<=2)
> > }
> >
> > #######################################
> > # An alternative contrasts function for ordered factors
> > # Ensures use of a difference penalty for such factors
> > #######################################
> > contr.diff <- function (n, contrasts = TRUE)
> > {
> >   if (is.numeric(n) && length(n) == 1) {
> >     if (n > 1)
> >       levs <- 1:n
> >     else stop("not enough degrees of freedom to define contrasts")
> >   }
> >   else {
> >     levs <- n
> >     n <- length(n)
> >   }
> >   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
> >   contr[outer(1:n,1:n, ">=")] <- 1
> >   if (n < 2)
> >     stop(gettextf("contrasts not defined for %d degrees of freedom",
> >                   n - 1), domain = NA)
> >   if (contrasts)
> >     contr <- contr[, -1, drop = FALSE]
> >   contr
> > }
> >
> >
> >
> > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch <
> [hidden email]>wrote:
> >
> >> On 14-01-25 6:05 PM, Axel Urbiz wrote:
> >>
> >>> Thanks again all. Essentially, this is the section of the code that is
> >>> causing trouble. This is part of the (exported) function which calls
> >>> contr.none (not exported). As mentioned, when I call the exported
> function
> >>> it complains with the error described before.
> >>>
> >>>
> >>>    oldcontrasts <- unlist(options("contrasts"))
> >>>      if (cts)
> >>>          options(contrasts = c(unordered = "contr.none", ordered =
> >>> "contr.diff"))
> >>>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
> >>>      options(contrasts = oldcontrasts)
> >>>
> >>
> >> This is hugely incomplete.  Please stop wasting everyone's time, and
> post
> >> something reproducible.
> >>
> >> Duncan Murdoch
> >>
> >>
> >
> >         [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Duncan Murdoch-2
On 14-01-28 6:32 AM, Axel Urbiz 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

>
> Thanks,
> Axel.
>
> ----
>
> #mycodefiles <- c("cmt.R")
> #package.skeleton(name = "foo", code_files = mycodefiles)
> #promptPackage("foo")
>
> #where cmt.R includes the code below:
>
> trt <- function(x) x
>
> cmt <- function(formula, data, subset, na.action = na.pass)  {
>    if (!inherits(formula, "formula"))
>      stop("Method is only for formula objects.")
>    mf <- match.call(expand.dots = FALSE)
>    args <- match(c("formula", "data", "subset", "na.action"),
>                  names(mf), 0)
>    mf <- mf[c(1, args)]
>    mf$drop.unused.levels <- TRUE
>    mf[[1]] <- as.name <http://as.name>("model.frame")
>    special <- "trt"
>    mt <- if(missing(data)) terms(formula, special) else terms(formula,
> special, data = data)
>    browser()
>    mf$formula <- mt
>    mf <- eval.parent(mf)
>    Terms <- attr(mf, "terms")
>    attr(Terms, "intercept") <- 0
>    trt.var <- attr(Terms, "specials")$trt
>    ct <- mf[, trt.var]
>    y <- model.response(mf, "numeric")
>    var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>    x <- model.matrix(terms(reformulate(var_names)),
>                      mf, contrasts)
>    intercept <- which(colnames(x) == "(Intercept)")
>    if (length(intercept > 0)) x <- x[, -intercept]
>    return(x)
>    }
>
>
>
>
> On Mon, Jan 27, 2014 at 2:42 AM, Henrik Bengtsson <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]
>     <mailto:[hidden email]>> wrote:
>      > Hi Duncan,
>      >
>      > My most sincere apologies. It's really not my intention to waste
>     anyones
>      > time. More the opposite...for some reason I thought that the
>     problem had to
>      > do with my call to options() and thought that would be enough. Here's
>      > something reproducible:
>      >
>      > I built a foo package based on the code under the "----" below.
>     In the
>      > namespace file, I've only exported: trt and cmt (not contr.none and
>      > contr.diff). Notice that cmt calls contr.none and contr.diff by
>     default.
>
>     As a start, try to export everything, particularly 'contr.none' and
>     'contr.diff' and see if that works.  Just a guess, but worth trying
>     out.
>
>     My $.02
>
>     /Henrik
>
>      >
>      > Then in R, I run this code and I get this error message:
>      >
>      > 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 in get(ctr, mode = "function", envir = parent.frame()) :
>      >    object 'contr.none' of mode 'function' was not found
>      >
>      > Thanks,
>      > Axel.
>      >
>      > --------------------------------------------
>      >
>      > trt <- function(x) x
>      >
>      > cmt <- function(formula, data, subset, na.action = na.pass, cts =
>     TRUE)  {
>      >
>      >   if (!inherits(formula, "formula"))
>      >     stop("Method is only for formula objects.")
>      >   mf <- match.call(expand.dots = FALSE)
>      >   args <- match(c("formula", "data", "subset", "na.action"),
>      >                 names(mf), 0)
>      >   mf <- mf[c(1, args)]
>      >   mf$drop.unused.levels <- TRUE
>      >   mf[[1]] <- as.name <http://as.name>("model.frame")
>      >   special <- "trt"
>      >   mt <- if(missing(data)) terms(formula, special) else terms(formula,
>      > special, data = data)
>      >   mf$formula <- mt
>      >   mf <- eval.parent(mf)
>      >   Terms <- attr(mf, "terms")
>      >   attr(Terms, "intercept") <- 0
>      >   trt.var <- attr(Terms, "specials")$trt
>      >   ct <- mf[, trt.var]
>      >   y <- model.response(mf, "numeric")
>      >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>      >   treat.names <- levels(as.factor(ct))
>      >   oldcontrasts <- unlist(options("contrasts"))
>      >   if (cts)
>      >     options(contrasts = c(unordered = "contr.none", ordered =
>     "contr.diff"))
>      >   x <- model.matrix(terms(reformulate(var_names)),
>      >                     mf, contrasts)
>      >   options(contrasts = oldcontrasts)
>      >   intercept <- which(colnames(x) == "(Intercept)")
>      >   if (length(intercept > 0)) x <- x[, -intercept]
>      >   return(x)
>      >   }
>      >
>      > #######################################
>      > # An alternative contrasts function for unordered factors
>      > # Ensures symmetric treatment of all levels of a factor
>      > #######################################
>      > contr.none <- function(n, contrasts) {
>      >   if (length(n) == 1)
>      >     contr.treatment(n, contrasts = n<=2)
>      >   else
>      >     contr.treatment(n, contrasts = length(unique(n))<=2)
>      > }
>      >
>      > #######################################
>      > # An alternative contrasts function for ordered factors
>      > # Ensures use of a difference penalty for such factors
>      > #######################################
>      > contr.diff <- function (n, contrasts = TRUE)
>      > {
>      >   if (is.numeric(n) && length(n) == 1) {
>      >     if (n > 1)
>      >       levs <- 1:n
>      >     else stop("not enough degrees of freedom to define contrasts")
>      >   }
>      >   else {
>      >     levs <- n
>      >     n <- length(n)
>      >   }
>      >   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
>      >   contr[outer(1:n,1:n, ">=")] <- 1
>      >   if (n < 2)
>      >     stop(gettextf("contrasts not defined for %d degrees of freedom",
>      >                   n - 1), domain = NA)
>      >   if (contrasts)
>      >     contr <- contr[, -1, drop = FALSE]
>      >   contr
>      > }
>      >
>      >
>      >
>      > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch
>     <[hidden email] <mailto:[hidden email]>>wrote:
>      >
>      >> On 14-01-25 6:05 PM, Axel Urbiz wrote:
>      >>
>      >>> Thanks again all. Essentially, this is the section of the code
>     that is
>      >>> causing trouble. This is part of the (exported) function which
>     calls
>      >>> contr.none (not exported). As mentioned, when I call the
>     exported function
>      >>> it complains with the error described before.
>      >>>
>      >>>
>      >>>    oldcontrasts <- unlist(options("contrasts"))
>      >>>      if (cts)
>      >>>          options(contrasts = c(unordered = "contr.none", ordered =
>      >>> "contr.diff"))
>      >>>      x <- model.matrix(terms(reformulate(var_names)), mf,
>     contrasts)
>      >>>      options(contrasts = oldcontrasts)
>      >>>
>      >>
>      >> This is hugely incomplete.  Please stop wasting everyone's time,
>     and post
>      >> something reproducible.
>      >>
>      >> Duncan Murdoch
>      >>
>      >>
>      >
>      >         [[alternative HTML version deleted]]
>      >
>      > ______________________________________________
>      > [hidden email] <mailto:[hidden email]> mailing list
>      > https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Axel Urbiz
Thanks for all your help on this Duncan. Much appreciated!

Regards
Axel

On Tuesday, January 28, 2014, Duncan Murdoch <[hidden email]>
wrote:

> On 14-01-28 6:32 AM, Axel Urbiz 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
>
>
>> Thanks,
>> Axel.
>>
>> ----
>>
>> #mycodefiles <- c("cmt.R")
>> #package.skeleton(name = "foo", code_files = mycodefiles)
>> #promptPackage("foo")
>>
>> #where cmt.R includes the code below:
>>
>> trt <- function(x) x
>>
>> cmt <- function(formula, data, subset, na.action = na.pass)  {
>>    if (!inherits(formula, "formula"))
>>      stop("Method is only for formula objects.")
>>    mf <- match.call(expand.dots = FALSE)
>>    args <- match(c("formula", "data", "subset", "na.action"),
>>                  names(mf), 0)
>>    mf <- mf[c(1, args)]
>>    mf$drop.unused.levels <- TRUE
>>    mf[[1]] <- as.name <http://as.name>("model.frame")
>>    special <- "trt"
>>    mt <- if(missing(data)) terms(formula, special) else terms(formula,
>> special, data = data)
>>    browser()
>>    mf$formula <- mt
>>    mf <- eval.parent(mf)
>>    Terms <- attr(mf, "terms")
>>    attr(Terms, "intercept") <- 0
>>    trt.var <- attr(Terms, "specials")$trt
>>    ct <- mf[, trt.var]
>>    y <- model.response(mf, "numeric")
>>    var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>>    x <- model.matrix(terms(reformulate(var_names)),
>>                      mf, contrasts)
>>    intercept <- which(colnames(x) == "(Intercept)")
>>    if (length(intercept > 0)) x <- x[, -intercept]
>>    return(x)
>>    }
>>
>>
>>
>>
>> On Mon, Jan 27, 2014 at 2:42 AM, Henrik Bengtsson <[hidden email]
>> <mailto:[hidden email]>> wrote:
>>
>>     On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]
>>     <mailto:[hidden email]>> wrote:
>>      > Hi Duncan,
>>      >
>>      > My most sincere apologies. It's really not my intention to waste
>>     anyones
>>      > time. More the opposite...for some reason I thought that the
>>     problem had to
>>      > do with my call to options() and thought that would be enough.
>> Here's
>>      > something reproducible:
>>      >
>>      > I built a foo package based on the code under the "----" below.
>>     In the
>>      > namespace file, I've only exported: trt and cmt (not contr.none and
>>      > contr.diff). Notice that cmt calls contr.none and contr.diff by
>>     default.
>>
>>     As a start, try to export everything, particularly 'contr.none' and
>>     'contr.diff' and see if that works.  Just a guess, but worth trying
>>     out.
>>
>>     My $.02
>>
>>     /Henrik
>>
>>      >
>>      > Then in R, I run this code and I get this error message:
>>      >
>>      > 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 in get(ctr, mode = "function", envir = parent.frame()) :
>>      >    object 'contr.none' of mode 'function' was not found
>>      >
>>      > Thanks,
>>      > Axel.
>>      >
>>      > --------------------------------------------
>>      >
>>      > trt <- function(x) x
>>      >
>>      > cmt <- function(formula, data, subset, na.action = na.pass, cts =
>>     TRUE)  {
>>      >
>>      >   if (!inherits(formula, "formula"))
>>      >     stop("Method is only for formula objects.")
>>      >   mf <- match.call(expand.dots = FALSE)
>>      >   args <- match(c("formula", "data", "subset", "na.action"),
>>      >                 names(mf), 0)
>>      >   mf <- mf[c(1, args)]
>>      >   mf$drop.unused.levels <- TRUE
>>      >   mf[[1]] <- as.name <http://as.name>("model.frame")
>>      >   special <- "trt"
>>      >   mt <- if(missing(data)) terms(formula, special) else
>> terms(formula,
>>      > special, data = data)
>>      >   mf$formula <- mt
>>      >   mf <- eval.parent(mf)
>>      >   Terms <- attr(mf, "terms")
>>      >   attr(Terms, "intercept") <- 0
>>      >   trt.var <- attr(Terms, "specials")$trt
>>      >   ct <- mf[, trt.var]
>>      >   y <- model.response(mf, "numeric")
>>      >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>>      >   treat.names <- levels(as.factor(ct))
>>      >   oldcontrasts <- unlist(options("contrasts"))
>>      >   if (cts)
>>      >     options(contrasts = c(unordered = "contr.none", ordered =
>>     "contr.diff"))
>>      >   x <- model.matrix(terms(reformulate(var_names)),
>>      >                     mf, contrasts)
>>      >   options(contrasts = oldcontrasts)
>>      >   intercept <- which(colnames(x) == "(Intercept)")
>>      >   if (length(intercept > 0)) x <- x[, -intercept]
>>      >   return(x)
>>      >   }
>>      >
>>      > #######################################
>>      > # An alternative contrasts function for unordered factors
>>      > # Ensures symmetric treatment of all levels of a factor
>>      > #######################################
>>      > contr.none <- function(n, contrasts) {
>>      >   if (length(n) == 1)
>>      >     contr.treatment(n, contrasts = n<=2)
>>      >   else
>>      >     contr.treatment(n, contrasts = length(unique(n))<=2)
>>      > }
>>      >
>>      > #######################################
>>      > # An alternative contrasts function for ordered factors
>>      > # Ensures use of a difference penalty for such factors
>>      > #######################################
>>      > contr.diff <- function (n, contrasts = TRUE)
>>      > {
>>      >   if (is.numeric(n) && length(n) == 1) {
>>      >     if (n > 1)
>>      >       levs <- 1:n
>>      >     else stop("not enough degrees of freedom to define contrasts")
>>      >   }
>>      >   else {
>>      >     levs <- n
>>      >     n <- length(n)
>>      >   }
>>      >   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
>>      >   contr[outer(1:n,1:n, ">=")] <- 1
>>      >   if (n < 2)
>>      >     stop(gettextf("contrasts not defined for %d degrees of
>> freedom",
>>      >                   n - 1), domain = NA)
>>      >   if (contrasts)
>>      >     contr <- contr[, -1, drop = FALSE]
>>      >   contr
>>      > }
>>      >
>>      >
>>      >
>>      > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch
>>     <[hidden email] <mailto:[hidden email]>>wrote:
>>      >
>>      >> On 14-01-25 6:05 PM, Axel Urbiz wrote:
>>      >>
>>      >>> Thanks again all. Essentially, this is the section of the code
>>     that is
>>      >>> causing trouble. This is part of the (exported) function which
>>     calls
>>      >>> contr.none (not exported). As mentioned, when I call the
>>     exported function
>>      >>> it complains with the error described before.
>>      >>>
>>      >>>
>>      >>>    oldcontrasts <- unlist(options("contrasts"))
>>      >>>      if (cts)
>>      >>>          options(contrasts = c(unordered = "contr.none", ordered
>> =
>>      >>> "contr.diff"))
>>      >>>      x <- model.matrix(terms(reformulate(var_names)), mf,
>>     contrasts)
>>      >>>      options(contrasts = oldcontrasts)
>>      >>>
>>      >>
>>      >> This is hugely incomplete.  Please stop wasting everyone's time,
>>     and post
>>      >> something reproducible.
>>      >>
>>      >> Duncan Murdoch
>>      >>
>>      >>
>>      >
>>      >         [[alternative HTML version deleted]]
>>      >
>>      > ______________________________________________
>>      > [hidden email] <mailto:[hidden email]> mailing list
>>      > https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>>
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

William Dunlap
In reply to this post by Axel Urbiz
You can more or less get what you want by reassigning the environment
of the formula to be a child of its original environment, where you put
your private functions in the new environment.  For example, do not
export trt and make the following change to your R code:  

% diff -u foo/R/cmt.R~ foo/R/cmt.R
--- foo/R/cmt.R~        2014-01-28 09:10:58.272711000 -0800
+++ foo/R/cmt.R 2014-01-28 09:09:06.299398000 -0800
@@ -1,9 +1,12 @@
-trt <- function(x) x
+trt <- function(x) { cat("Calling foo:::trt\n"); x }

 cmt <- function(formula, data, subset, na.action = na.pass)  {

   if (!inherits(formula, "formula"))
     stop("Method is only for formula objects.")
+  intercalatedEnvir <- new.env(parent=environment(formula))
+  intercalatedEnvir$trt <- trt
+  environment(formula) <- intercalatedEnvir
   mf <- match.call(expand.dots = FALSE)
   args <- match(c("formula", "data", "subset", "na.action"),
                 names(mf), 0)

Bill Dunlap
TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]] On Behalf
> Of Axel Urbiz
> Sent: Tuesday, January 28, 2014 3:33 AM
> To: Henrik Bengtsson
> Cc: r-devel
> Subject: Re: [Rd] package NAMESPACE question
>
> 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.
>
> Thanks,
> Axel.
>
> ----
>
> #mycodefiles <- c("cmt.R")
> #package.skeleton(name = "foo", code_files = mycodefiles)
> #promptPackage("foo")
>
> #where cmt.R includes the code below:
>
> trt <- function(x) x
>
> cmt <- function(formula, data, subset, na.action = na.pass)  {
>
>   if (!inherits(formula, "formula"))
>     stop("Method is only for formula objects.")
>   mf <- match.call(expand.dots = FALSE)
>   args <- match(c("formula", "data", "subset", "na.action"),
>                 names(mf), 0)
>   mf <- mf[c(1, args)]
>   mf$drop.unused.levels <- TRUE
>   mf[[1]] <- as.name("model.frame")
>   special <- "trt"
>   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> special, data = data)
>   browser()
>   mf$formula <- mt
>   mf <- eval.parent(mf)
>   Terms <- attr(mf, "terms")
>   attr(Terms, "intercept") <- 0
>   trt.var <- attr(Terms, "specials")$trt
>   ct <- mf[, trt.var]
>   y <- model.response(mf, "numeric")
>   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
>   x <- model.matrix(terms(reformulate(var_names)),
>                     mf, contrasts)
>   intercept <- which(colnames(x) == "(Intercept)")
>   if (length(intercept > 0)) x <- x[, -intercept]
>   return(x)
>   }
>
>
>
>
> On Mon, Jan 27, 2014 at 2:42 AM, Henrik Bengtsson <[hidden email]>wrote:
>
> > On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]> wrote:
> > > Hi Duncan,
> > >
> > > My most sincere apologies. It's really not my intention to waste anyones
> > > time. More the opposite...for some reason I thought that the problem had
> > to
> > > do with my call to options() and thought that would be enough. Here's
> > > something reproducible:
> > >
> > > I built a foo package based on the code under the "----" below. In the
> > > namespace file, I've only exported: trt and cmt (not contr.none and
> > > contr.diff). Notice that cmt calls contr.none and contr.diff by default.
> >
> > As a start, try to export everything, particularly 'contr.none' and
> > 'contr.diff' and see if that works.  Just a guess, but worth trying
> > out.
> >
> > My $.02
> >
> > /Henrik
> >
> > >
> > > Then in R, I run this code and I get this error message:
> > >
> > > 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 in get(ctr, mode = "function", envir = parent.frame()) :
> > >    object 'contr.none' of mode 'function' was not found
> > >
> > > Thanks,
> > > Axel.
> > >
> > > --------------------------------------------
> > >
> > > trt <- function(x) x
> > >
> > > cmt <- function(formula, data, subset, na.action = na.pass, cts = TRUE)
> >  {
> > >
> > >   if (!inherits(formula, "formula"))
> > >     stop("Method is only for formula objects.")
> > >   mf <- match.call(expand.dots = FALSE)
> > >   args <- match(c("formula", "data", "subset", "na.action"),
> > >                 names(mf), 0)
> > >   mf <- mf[c(1, args)]
> > >   mf$drop.unused.levels <- TRUE
> > >   mf[[1]] <- as.name("model.frame")
> > >   special <- "trt"
> > >   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> > > special, data = data)
> > >   mf$formula <- mt
> > >   mf <- eval.parent(mf)
> > >   Terms <- attr(mf, "terms")
> > >   attr(Terms, "intercept") <- 0
> > >   trt.var <- attr(Terms, "specials")$trt
> > >   ct <- mf[, trt.var]
> > >   y <- model.response(mf, "numeric")
> > >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
> > >   treat.names <- levels(as.factor(ct))
> > >   oldcontrasts <- unlist(options("contrasts"))
> > >   if (cts)
> > >     options(contrasts = c(unordered = "contr.none", ordered =
> > "contr.diff"))
> > >   x <- model.matrix(terms(reformulate(var_names)),
> > >                     mf, contrasts)
> > >   options(contrasts = oldcontrasts)
> > >   intercept <- which(colnames(x) == "(Intercept)")
> > >   if (length(intercept > 0)) x <- x[, -intercept]
> > >   return(x)
> > >   }
> > >
> > > #######################################
> > > # An alternative contrasts function for unordered factors
> > > # Ensures symmetric treatment of all levels of a factor
> > > #######################################
> > > contr.none <- function(n, contrasts) {
> > >   if (length(n) == 1)
> > >     contr.treatment(n, contrasts = n<=2)
> > >   else
> > >     contr.treatment(n, contrasts = length(unique(n))<=2)
> > > }
> > >
> > > #######################################
> > > # An alternative contrasts function for ordered factors
> > > # Ensures use of a difference penalty for such factors
> > > #######################################
> > > contr.diff <- function (n, contrasts = TRUE)
> > > {
> > >   if (is.numeric(n) && length(n) == 1) {
> > >     if (n > 1)
> > >       levs <- 1:n
> > >     else stop("not enough degrees of freedom to define contrasts")
> > >   }
> > >   else {
> > >     levs <- n
> > >     n <- length(n)
> > >   }
> > >   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
> > >   contr[outer(1:n,1:n, ">=")] <- 1
> > >   if (n < 2)
> > >     stop(gettextf("contrasts not defined for %d degrees of freedom",
> > >                   n - 1), domain = NA)
> > >   if (contrasts)
> > >     contr <- contr[, -1, drop = FALSE]
> > >   contr
> > > }
> > >
> > >
> > >
> > > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch <
> > [hidden email]>wrote:
> > >
> > >> On 14-01-25 6:05 PM, Axel Urbiz wrote:
> > >>
> > >>> Thanks again all. Essentially, this is the section of the code that is
> > >>> causing trouble. This is part of the (exported) function which calls
> > >>> contr.none (not exported). As mentioned, when I call the exported
> > function
> > >>> it complains with the error described before.
> > >>>
> > >>>
> > >>>    oldcontrasts <- unlist(options("contrasts"))
> > >>>      if (cts)
> > >>>          options(contrasts = c(unordered = "contr.none", ordered =
> > >>> "contr.diff"))
> > >>>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
> > >>>      options(contrasts = oldcontrasts)
> > >>>
> > >>
> > >> This is hugely incomplete.  Please stop wasting everyone's time, and
> > post
> > >> something reproducible.
> > >>
> > >> Duncan Murdoch
> > >>
> > >>
> > >
> > >         [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] mailing list
> > > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: package NAMESPACE question

Duncan Murdoch-2
On 28/01/2014 12:15 PM, William Dunlap wrote:

> You can more or less get what you want by reassigning the environment
> of the formula to be a child of its original environment, where you put
> your private functions in the new environment.  For example, do not
> export trt and make the following change to your R code:
>
> % diff -u foo/R/cmt.R~ foo/R/cmt.R
> --- foo/R/cmt.R~        2014-01-28 09:10:58.272711000 -0800
> +++ foo/R/cmt.R 2014-01-28 09:09:06.299398000 -0800
> @@ -1,9 +1,12 @@
> -trt <- function(x) x
> +trt <- function(x) { cat("Calling foo:::trt\n"); x }
>
>   cmt <- function(formula, data, subset, na.action = na.pass)  {
>
>     if (!inherits(formula, "formula"))
>       stop("Method is only for formula objects.")
> +  intercalatedEnvir <- new.env(parent=environment(formula))
> +  intercalatedEnvir$trt <- trt
> +  environment(formula) <- intercalatedEnvir
>     mf <- match.call(expand.dots = FALSE)
>     args <- match(c("formula", "data", "subset", "na.action"),
>                   names(mf), 0)

I'm not so sure this is the right solution.  This means that if users
have their own function named trt, the one in the namespace will take
precedence.  In most cases users don't want that.

The problem is that in order to make the user one take precedence, you
could modify the parent of the environment of the formula, but that's
bad too (because it will have global effects since environments are
reference objects).

I don't see a good solution here.  I would change the requirements.

Duncan Murdoch

>
> Bill Dunlap
> TIBCO Software
> wdunlap tibco.com
>
>
> > -----Original Message-----
> > From: [hidden email] [mailto:[hidden email]] On Behalf
> > Of Axel Urbiz
> > Sent: Tuesday, January 28, 2014 3:33 AM
> > To: Henrik Bengtsson
> > Cc: r-devel
> > Subject: Re: [Rd] package NAMESPACE question
> >
> > 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.
> >
> > Thanks,
> > Axel.
> >
> > ----
> >
> > #mycodefiles <- c("cmt.R")
> > #package.skeleton(name = "foo", code_files = mycodefiles)
> > #promptPackage("foo")
> >
> > #where cmt.R includes the code below:
> >
> > trt <- function(x) x
> >
> > cmt <- function(formula, data, subset, na.action = na.pass)  {
> >
> >   if (!inherits(formula, "formula"))
> >     stop("Method is only for formula objects.")
> >   mf <- match.call(expand.dots = FALSE)
> >   args <- match(c("formula", "data", "subset", "na.action"),
> >                 names(mf), 0)
> >   mf <- mf[c(1, args)]
> >   mf$drop.unused.levels <- TRUE
> >   mf[[1]] <- as.name("model.frame")
> >   special <- "trt"
> >   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> > special, data = data)
> >   browser()
> >   mf$formula <- mt
> >   mf <- eval.parent(mf)
> >   Terms <- attr(mf, "terms")
> >   attr(Terms, "intercept") <- 0
> >   trt.var <- attr(Terms, "specials")$trt
> >   ct <- mf[, trt.var]
> >   y <- model.response(mf, "numeric")
> >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
> >   x <- model.matrix(terms(reformulate(var_names)),
> >                     mf, contrasts)
> >   intercept <- which(colnames(x) == "(Intercept)")
> >   if (length(intercept > 0)) x <- x[, -intercept]
> >   return(x)
> >   }
> >
> >
> >
> >
> > On Mon, Jan 27, 2014 at 2:42 AM, Henrik Bengtsson <[hidden email]>wrote:
> >
> > > On Sun, Jan 26, 2014 at 6:34 AM, Axel Urbiz <[hidden email]> wrote:
> > > > Hi Duncan,
> > > >
> > > > My most sincere apologies. It's really not my intention to waste anyones
> > > > time. More the opposite...for some reason I thought that the problem had
> > > to
> > > > do with my call to options() and thought that would be enough. Here's
> > > > something reproducible:
> > > >
> > > > I built a foo package based on the code under the "----" below. In the
> > > > namespace file, I've only exported: trt and cmt (not contr.none and
> > > > contr.diff). Notice that cmt calls contr.none and contr.diff by default.
> > >
> > > As a start, try to export everything, particularly 'contr.none' and
> > > 'contr.diff' and see if that works.  Just a guess, but worth trying
> > > out.
> > >
> > > My $.02
> > >
> > > /Henrik
> > >
> > > >
> > > > Then in R, I run this code and I get this error message:
> > > >
> > > > 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 in get(ctr, mode = "function", envir = parent.frame()) :
> > > >    object 'contr.none' of mode 'function' was not found
> > > >
> > > > Thanks,
> > > > Axel.
> > > >
> > > > --------------------------------------------
> > > >
> > > > trt <- function(x) x
> > > >
> > > > cmt <- function(formula, data, subset, na.action = na.pass, cts = TRUE)
> > >  {
> > > >
> > > >   if (!inherits(formula, "formula"))
> > > >     stop("Method is only for formula objects.")
> > > >   mf <- match.call(expand.dots = FALSE)
> > > >   args <- match(c("formula", "data", "subset", "na.action"),
> > > >                 names(mf), 0)
> > > >   mf <- mf[c(1, args)]
> > > >   mf$drop.unused.levels <- TRUE
> > > >   mf[[1]] <- as.name("model.frame")
> > > >   special <- "trt"
> > > >   mt <- if(missing(data)) terms(formula, special) else terms(formula,
> > > > special, data = data)
> > > >   mf$formula <- mt
> > > >   mf <- eval.parent(mf)
> > > >   Terms <- attr(mf, "terms")
> > > >   attr(Terms, "intercept") <- 0
> > > >   trt.var <- attr(Terms, "specials")$trt
> > > >   ct <- mf[, trt.var]
> > > >   y <- model.response(mf, "numeric")
> > > >   var_names <- attributes(Terms)$term.labels[-(trt.var-1)]
> > > >   treat.names <- levels(as.factor(ct))
> > > >   oldcontrasts <- unlist(options("contrasts"))
> > > >   if (cts)
> > > >     options(contrasts = c(unordered = "contr.none", ordered =
> > > "contr.diff"))
> > > >   x <- model.matrix(terms(reformulate(var_names)),
> > > >                     mf, contrasts)
> > > >   options(contrasts = oldcontrasts)
> > > >   intercept <- which(colnames(x) == "(Intercept)")
> > > >   if (length(intercept > 0)) x <- x[, -intercept]
> > > >   return(x)
> > > >   }
> > > >
> > > > #######################################
> > > > # An alternative contrasts function for unordered factors
> > > > # Ensures symmetric treatment of all levels of a factor
> > > > #######################################
> > > > contr.none <- function(n, contrasts) {
> > > >   if (length(n) == 1)
> > > >     contr.treatment(n, contrasts = n<=2)
> > > >   else
> > > >     contr.treatment(n, contrasts = length(unique(n))<=2)
> > > > }
> > > >
> > > > #######################################
> > > > # An alternative contrasts function for ordered factors
> > > > # Ensures use of a difference penalty for such factors
> > > > #######################################
> > > > contr.diff <- function (n, contrasts = TRUE)
> > > > {
> > > >   if (is.numeric(n) && length(n) == 1) {
> > > >     if (n > 1)
> > > >       levs <- 1:n
> > > >     else stop("not enough degrees of freedom to define contrasts")
> > > >   }
> > > >   else {
> > > >     levs <- n
> > > >     n <- length(n)
> > > >   }
> > > >   contr <- array(0, c(n, n), list(levs, paste(">=", levs, sep="")))
> > > >   contr[outer(1:n,1:n, ">=")] <- 1
> > > >   if (n < 2)
> > > >     stop(gettextf("contrasts not defined for %d degrees of freedom",
> > > >                   n - 1), domain = NA)
> > > >   if (contrasts)
> > > >     contr <- contr[, -1, drop = FALSE]
> > > >   contr
> > > > }
> > > >
> > > >
> > > >
> > > > On Sun, Jan 26, 2014 at 1:21 PM, Duncan Murdoch <
> > > [hidden email]>wrote:
> > > >
> > > >> On 14-01-25 6:05 PM, Axel Urbiz wrote:
> > > >>
> > > >>> Thanks again all. Essentially, this is the section of the code that is
> > > >>> causing trouble. This is part of the (exported) function which calls
> > > >>> contr.none (not exported). As mentioned, when I call the exported
> > > function
> > > >>> it complains with the error described before.
> > > >>>
> > > >>>
> > > >>>    oldcontrasts <- unlist(options("contrasts"))
> > > >>>      if (cts)
> > > >>>          options(contrasts = c(unordered = "contr.none", ordered =
> > > >>> "contr.diff"))
> > > >>>      x <- model.matrix(terms(reformulate(var_names)), mf, contrasts)
> > > >>>      options(contrasts = oldcontrasts)
> > > >>>
> > > >>
> > > >> This is hugely incomplete.  Please stop wasting everyone's time, and
> > > post
> > > >> something reproducible.
> > > >>
> > > >> Duncan Murdoch
> > > >>
> > > >>
> > > >
> > > >         [[alternative HTML version deleted]]
> > > >
> > > > ______________________________________________
> > > > [hidden email] mailing list
> > > > https://stat.ethz.ch/mailman/listinfo/r-devel
> > >
> >
> > [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
12