[Rd] Class not found when search in .onLoad

2011-06-23 Thread Renaud Gaujoux

Hi,

I am facing with a strange behaviour of isClass and extends when these 
are called in .onLoad in both R 2.12.1 and R 2.13.0. This is preventing 
my package from doing some object initializations at a proper place 
(i.e. in .onLoad).


Suppose one defines two S4 classes in a package, and that one needs to 
check the inheritance between these two when loading the package (e.g. 
to validate slots in objects).

See package attached or code below (not sure attachments can go through).

in R 2.13.0:
At the loading check after installation, the classes are not found by 
`isClass` and `extends` when these are called in .onLoad, but are 
correctly found when called in .onAttach.
However findClass correctly finds the class in both case, as well as 
isClass if it is called with the argument 
`where=asNamespace('')`.
When the package is loaded from an open R session, the behaviour is the 
same.


in R 2.12.1:
the classes are correctly found by isClass and extends when these are 
called in .onLoad or .onAttach, but only at installation (i.e. at the 
loading check after R CMD INSTALL).
When the package is loaded from an open R session, one fails to find the 
classes only in .onLoad while in .onAttach they are correctly found.


This is really an issue as up to now I was using .onAttach to do my 
checks and initialization, but it is not a wise thing as package that 
would only need to import my package (load and not attach) will not get 
internal objects properly initialized. All this should be done in 
.onLoad, but I cannot do it due to this behaviour of `extends`.


Can someone provide some explanations or work around.

Thank you,
Renaud



# script.R


setClass('A', representation(data='numeric'))

setClass('B', contain='A')

check.classes <- function(){

a <- new('A')
b <- new('B')

message("isClass('A'): ", methods::isClass('A'))
message("isClass('A') in namespace: ", methods::isClass('A', 
where=asNamespace('anRpackage')))

message("findClass('A'): ")
print(methods::findClass('A'))

message("isClass('B'): ", methods::isClass('B'))
message("isClass('B') in namespace: ", methods::isClass('B', 
where=asNamespace('anRpackage')))

message("findClass('B'): ")
print(methods::findClass('B'))

message("extends('B', 'A'): ", methods::extends('B', 'A'))
message("is(a, 'A'): ", is(a, 'A'))
message("inherits(a, 'A'): ", inherits(a, 'A'))
message("is(b, 'A'): ", is(b, 'A'))
}

.onLoad <- function(libname, pkgname=NULL){
cat("\n## .onLoad ##\n")
check.classes()
}

.onAttach <- function(libname, pkgname){
cat("\n## .onAttach ##\n")
check.classes()
}






# Output




# R-2.13.0 CMD INSTALL anRpackage_1.0.tar.gz


* installing *source* package ‘anRpackage’ ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

## .onLoad ##
isClass('A'): FALSE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


isClass('B'): FALSE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]


extends('B', 'A'): FALSE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]


extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

* DONE (anRpackage)



# From a R-2.12.1 Console


## .onLoad ##
isClass('A'): FALSE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


isClass('B'): FALSE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]


extends('B', 'A'): FALSE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]


extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE



# R-2.12.1 CMD INSTALL anRpackage_1.0.tar.gz


* installing *source* package ‘anRpackage’ ...
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices ...
** testing if installed package can be loaded

## .onLoad ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


isClass('B'): TRUE
isClass('B') in namespace: TRUE
findClass('B'):
[[1]]


extends('B', 'A'): TRUE
is(a, 'A'): TRUE
inherits(a, 'A'): TRUE
is(b, 'A'): TRUE

## .onAttach ##
isClass('A'): TRUE
isClass('A') in namespace: TRUE
findClass('A'):
[[1]]


Re: [Rd] First package submission to CRAN

2011-06-23 Thread steven mosher
Thank you david.. with R CMD build --binary   I was seeing the deprecated
warning


On Wed, Jun 22, 2011 at 4:51 PM, David Scott  wrote:

>  On 23/06/11 08:34, Christophe Dutang wrote:
>
>> Hi,
>>
>> By default, R CMD build makes sources, you have to use --binary if you
>> want to get binaries. But you have to submit sources to the CRAN ftp server
>> (and not binary). So just run a R CMD build.
>>
>> C
>>
>> --
>> Christophe Dutang
>> Ph.D. student at ISFA, Lyon, France
>> website: http://dutangc.free.fr
>>
>
> That is now deprecated Christophe. Recommended now is
>
> R CMD INSTALL --build
>
> to get a binary. See the recent thread with the subject
>
> Porting "unmaintained" packages to post R 2.10.0 era
>
> David Scott
>
>
>  Le 22 juin 2011 à 22:12, steven mosher a écrit :
>>
>>  I'm preparing to submit my first package to CRAN, thanks to the help of
>>> too
>>> many people to mention.
>>>
>>> I've built and checked the package on Windows  ( making a zip) and my
>>> path
>>> points to the 64 bit version of R.
>>>
>>> Everything builds and checks and the final warnings have been fixed. My
>>> package is pure R with no source from
>>>
>>> other languages.  My questions are  as follows. I've read the docs and
>>> just
>>> need a bit of clarification.
>>>
>>> 1. For submission I should just build source  R CMD build mypkg  which
>>> outputs a tar.gz
>>> 2. Do I have to/ how do I build for 32 bit?
>>>
>>> Thanks,
>>>
>>> Steve
>>>
>>>[[alternative HTML version deleted]]
>>>
>>> __**
>>> R-devel@r-project.org mailing list
>>> https://stat.ethz.ch/mailman/**listinfo/r-devel
>>>
>> __**
>> R-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-devel
>>
>
>
> --
> __**__**_
> David Scott Department of Statistics
>The University of Auckland, PB 92019
>Auckland 1142,NEW ZEALAND
> Phone: +64 9 923 5055, or +64 9 373 7599 ext 85055
> Email:  d.sc...@auckland.ac.nz,  Fax: +64 9 373 7018
>
>
>

[[alternative HTML version deleted]]

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


Re: [Rd] Class not found when search in .onLoad

2011-06-23 Thread John Chambers
The workaround is to use the package's namespace, as you did.  That's 
one of the reasons why pkgname is an argument to .onLoad().


Depending on what you want to do, you can either use the namespace as an 
argument where= or get the class definition from the namespace and use 
it in place of the class name.


A side advantage is that such checks work regardless of whether or not 
the classes, etc. are exported from the package.  Also, on the remote 
chance there is another class of the same name, the check works 
unambiguously on your package's version.


The relevant part of your script, modified accordingly, seems to work as 
desired.


John


# script.R


setClass('A', representation(data='numeric'))

setClass('B', contains='A') # the argument is contains=

check.classes <- function(where){

message("isClass('A', where = where): ", methods::isClass('A', where = 
where))


message("isClass('B', where = where): ", methods::isClass('B', where = 
where))


classA <- getClass('A', where = where)
classB <- getClass('B', where = where)
message("extends(classB, classA): ", methods::extends(classB, classA))
}

.onLoad <- function(libname, pkgname=NULL){
cat("\n## .onLoad ##\n")
check.classes(asNamespace(pkgname))
}

.onAttach <- function(libname, pkgname){
cat("\n## .onAttach ##\n")
check.classes(asNamespace(pkgname))
}




On 6/23/11 4:22 AM, Renaud Gaujoux wrote:

Hi,

I am facing with a strange behaviour of isClass and extends when these
are called in .onLoad in both R 2.12.1 and R 2.13.0. This is preventing
my package from doing some object initializations at a proper place
(i.e. in .onLoad).

Suppose one defines two S4 classes in a package, and that one needs to
check the inheritance between these two when loading the package (e.g.
to validate slots in objects).
See package attached or code below (not sure attachments can go through).

in R 2.13.0:
At the loading check after installation, the classes are not found by
`isClass` and `extends` when these are called in .onLoad, but are
correctly found when called in .onAttach.
However findClass correctly finds the class in both case, as well as
isClass if it is called with the argument
`where=asNamespace('')`.
When the package is loaded from an open R session, the behaviour is the
same.

in R 2.12.1:
the classes are correctly found by isClass and extends when these are
called in .onLoad or .onAttach, but only at installation (i.e. at the
loading check after R CMD INSTALL).
When the package is loaded from an open R session, one fails to find the
classes only in .onLoad while in .onAttach they are correctly found.

This is really an issue as up to now I was using .onAttach to do my
checks and initialization, but it is not a wise thing as package that
would only need to import my package (load and not attach) will not get
internal objects properly initialized. All this should be done in
.onLoad, but I cannot do it due to this behaviour of `extends`.

Can someone provide some explanations or work around.

Thank you,
Renaud



# script.R


setClass('A', representation(data='numeric'))

setClass('B', contain='A')

check.classes <- function(){

a <- new('A')
b <- new('B')

message("isClass('A'): ", methods::isClass('A'))
message("isClass('A') in namespace: ", methods::isClass('A',
where=asNamespace('anRpackage')))
message("findClass('A'): ")
print(methods::findClass('A'))

message("isClass('B'): ", methods::isClass('B'))
message("isClass('B') in namespace: ", methods::isClass('B',
where=asNamespace('anRpackage')))
message("findClass('B'): ")
print(methods::findClass('B'))

message("extends('B', 'A'): ", methods::extends('B', 'A'))
message("is(a, 'A'): ", is(a, 'A'))
message("inherits(a, 'A'): ", inherits(a, 'A'))
message("is(b, 'A'): ", is(b, 'A'))
}

.onLoad <- function(libname, pkgname=NULL){
cat("\n## .onLoad ##\n")
check.classes()
}

.onAttach <- function(libname, pkgname){
cat("\n## .onAttach ##\n")
check.classes()
}




..


###
UNIVERSITY OF CAPE TOWN
This e-mail is subject to the UCT ICT policies and e-mail disclaimer
published on our website at
http://www.uct.ac.za/about/policies/emaildisclaimer/ or obtainable from
+27 21 650 9111. This e-mail is intended only for the person(s) to whom
it is addressed. If the e-mail has reached you in error, please notify
the author. If you are not the intended recipient of the e-mail you may
not use, disclose, copy, redirect or print the content. If this e-mail
is not related to the business of UCT it is sent by the sender in the
sender's individual capacity.

###




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


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


Re: [Rd] First package submission to CRAN

2011-06-23 Thread Sascha Vieweg

On 11-06-22 22:12, steven mosher wrote:

I'm preparing to submit my first package to CRAN, thanks to the 
help of too many people to mention.


[...]

My package is pure R with no source from other languages.  My 
questions are as follows. I've read the docs and just need a bit 
of clarification.


1. For submission I should just build source R CMD build mypkg
   which outputs a tar.gz

2. Do I have to/ how do I build for 32 bit?


AFAIK there is no need to care about 32/64 bit as long as your 
package is pure R code.


Congrats to your new package!

*S*

--
Sascha Vieweg, saschav...@gmail.com

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


Re: [Rd] First package submission to CRAN

2011-06-23 Thread steven mosher
 Thanks. figured as much and submitted

On Thu, Jun 23, 2011 at 10:16 AM, Sascha Vieweg wrote:

> On 11-06-22 22:12, steven mosher wrote:
>
>  I'm preparing to submit my first package to CRAN, thanks to the help of
>> too many people to mention.
>>
>
> [...]
>
>
>  My package is pure R with no source from other languages.  My questions
>> are as follows. I've read the docs and just need a bit of clarification.
>>
>> 1. For submission I should just build source R CMD build mypkg
>>   which outputs a tar.gz
>>
>> 2. Do I have to/ how do I build for 32 bit?
>>
>
> AFAIK there is no need to care about 32/64 bit as long as your package is
> pure R code.
>
> Congrats to your new package!
>
> *S*
>
> --
> Sascha Vieweg, saschav...@gmail.com
>

[[alternative HTML version deleted]]

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


[Rd] Unexpected email address change - and maybe a missing manual patch

2011-06-23 Thread Stephen Ellison
Please forgive the unorthodox posting, but some marketing genius here has 
decided I need a new corporate email address and has duly changed my outgoing 
email without adequate notice to amend listserve subscriptions. The result is a 
lot of unexpected bounces from R-help and R-devel. I'll try to fix that in 
time, but more importantly a couple of you have been corresponding with me re a 
proposed patch for a manual. I sent the patch a couple of days back, so if it 
hasn;t arrived, it's probably got stuck behind a spam trap. If so, please let 
me know, add the correct address to your spam filter and I'll try again ...


In the mean time I'll try to sort out the resulting listserve subscription mess.


Steve Ellison
LGC
***
This email and any attachments are confidential. Any use...{{dropped:8}}

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