Dear listers, I am making a trial to move from S3 to S4... I have created some classes of interest and they work acceptably well for the purpose. I am now wondering how to make them operate in a package. In clear when a package is loaded (eg library(mypackage)) where should I put the class descriptions: setClass("Prior",representation(Distrib="character",Params="list")) setClass("SamplePrior",representation("Prior",Sample="list")) so that they are created and then usable for functions after a simple call to library(mypackage). It is probably something trivial, but I could not find out something clear on this (eg example) in the R-help-list, writing R-extensions, nor in S programming... and trying to get this info through reading other library codes has been unsuccessful. Patrick
The key steps are 1) setClass and setMethod calls are executed at the top level in your package See e.g. file mle.R in the stats4 package which ships with R. 2) The package is installed with SaveImage: yes or LazyLoad: yes. The stats4 package is shipped with R partly as an example of using S4 classes. On Sun, 5 Mar 2006, Patrick Giraudoux wrote:> Dear listers, > > I am making a trial to move from S3 to S4... I have created some classes > of interest and they work acceptably well for the purpose. I am now > wondering how to make them operate in a package. In clear when a package > is loaded (eg library(mypackage)) where should I put the class descriptions: > > setClass("Prior",representation(Distrib="character",Params="list")) > setClass("SamplePrior",representation("Prior",Sample="list")) > > so that they are created and then usable for functions after a simple > call to library(mypackage). > > It is probably something trivial, but I could not find out something > clear on this (eg example) in the R-help-list, writing R-extensions, nor > in S programming... and trying to get this info through reading other > library codes has been unsuccessful. > > Patrick > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
OK. Looks like I have got it... going through the source code of sp, classes are declared as usual functions in *.R files. With the current example, suppose I must write two files of names "class-Prior.R" and "class-SamplePrior.R" including setClass("Prior",representation(Distrib="character",Params="list")) and setClass("SamplePrior",representation("Prior",Sample="list")) respectively. Suppose the alphabetical order of each file name may be important somehow, since "SamplePrior" cannot be defined without a prior definition of "Prior" Can anybody confirm? If so, I can go on with validity checking functions... Patrick Patrick Giraudoux a ?crit :> Dear listers, > > I am making a trial to move from S3 to S4... I have created some > classes of interest and they work acceptably well for the purpose. I > am now wondering how to make them operate in a package. In clear when > a package is loaded (eg library(mypackage)) where should I put the > class descriptions: > > setClass("Prior",representation(Distrib="character",Params="list")) > setClass("SamplePrior",representation("Prior",Sample="list")) > > so that they are created and then usable for functions after a simple > call to library(mypackage). > > It is probably something trivial, but I could not find out something > clear on this (eg example) in the R-help-list, writing R-extensions, > nor in S programming... and trying to get this info through reading > other library codes has been unsuccessful. > > Patrick > > >
On Sun, 5 Mar 2006, Patrick Giraudoux wrote:> OK. Looks like I have got it... going through the source code of sp, > classes are declared as usual functions in *.R files. With the current > example, suppose I must write two files of names "class-Prior.R" and > "class-SamplePrior.R" including > setClass("Prior",representation(Distrib="character",Params="list")) and > setClass("SamplePrior",representation("Prior",Sample="list")) respectively. > > Suppose the alphabetical order of each file name may be important > somehow, since "SamplePrior" cannot be defined without a prior > definition of "Prior" > > Can anybody confirm?Indeed. I suggest that you just use one file for all related classes and methods. How R code in a package is divided amongst files is just a matter of convenience for you, the maintainer. They will all be concatenated before use. (The order is alphabetic in the C locale unless you override it via the Collate: field in the DESCRIPTION file.)> > If so, I can go on with validity checking functions... > > Patrick > > Patrick Giraudoux a ?crit : >> Dear listers, >> >> I am making a trial to move from S3 to S4... I have created some >> classes of interest and they work acceptably well for the purpose. I >> am now wondering how to make them operate in a package. In clear when >> a package is loaded (eg library(mypackage)) where should I put the >> class descriptions: >> >> setClass("Prior",representation(Distrib="character",Params="list")) >> setClass("SamplePrior",representation("Prior",Sample="list")) >> >> so that they are created and then usable for functions after a simple >> call to library(mypackage). >> >> It is probably something trivial, but I could not find out something >> clear on this (eg example) in the R-help-list, writing R-extensions, >> nor in S programming... and trying to get this info through reading >> other library codes has been unsuccessful. >> >> Patrick >> >> >> > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595