|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
class export in package creation / setClass / namespace?Dear all,
I have been trying to create an R package. This has been successfull until I tried to define classes. Currently, my procedure is the following: Start R, load the function and class definition >tmp <- function (x) {x} >setClass("rpa", contains = "list", where=topenv(parent.frame())) Use package skeleton to create directory structure: >package.skeleton(name = "test",list=c("tmp")) Edit man files, add test/NAMESPACE file with the following contents: >export(tmp) >exportClasses("rpa") Use R CMD check and R CMD build to create tarball. -> No error messages. Start R, install the created package: >install.packages("test_1.0.tar.gz",repos=NULL) Test the new package: >require(test) >my.object = new("rpa") This gives the error message >Error in getClass(Class, where = topenv(parent.frame())) : > "rpa" is not a defined class Any tips on how to define and export global classes in package creation? I also tried - to replace the 'topenv(parent.frame())' with '.GlobalEnv' in setClass. - place the setClass within function 'tmp' - exportClasses(rpa) in the namespace (without suffixes "") I paged through the related mailing list discussions but could not find a solution. Any tips and tricks would be worthwhile now; I guess this should be a standard issue but it seems a bit tricky to come up with a quick solution with the little programming experience that I have. kind regards Leo Lahti [[alternative HTML version deleted]] ______________________________________________ R-help@... mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
|
|
|
|
Re: class export in package creation / setClass / namespace?Ok, I could solve also the latter problem by defining show.myclass function
in the zzz.R file and adding the line 'S3method(show,myclass)' into NAMESPACE file. Now the package passes all checks. The information on how to exactly extend existing methods and include new methods/classes into a package is available but rather scattered in the web. A step-by-step tutorial written by an experienced user who is aware of best practices etc. would be rather useful for a beginner. best regards Leo On Mon, Jul 20, 2009 at 7:09 PM, L L <lmlahti@...> wrote: > Thanks, the issue was solved by adding class definitions to the zzz.R file > in the R code directory. However, this led to a new problem. > > The zzz.R now contains class definition: > > > setClass("myclass", contains = "list") > > and method definition for the new class, extending the generic 'show': > > > setGeneric("show",function(x,...){standardGeneric("show")}) > > setMethod("show", "myclass",function(x, ...) {cat("myclass object \n")}) > > I get two warnings. The first one: > > * checking Rd files ... WARNING > Rd files with duplicated alias 'show,myclass-method': > myclass-class.Rd show-methods.Rd > > I get this one because the alias row for the method > (\alias{show,myclass-method}) is in both myclass-class.Rd file and > show-methods.Rd file (created by promptMethods function). This is likely > related to the second warning: > > * checking for missing documentation entries ... WARNING > Undocumented code objects: > show > > I thought that the show method would've been documented correctly as I put > 'show-methods.Rd' file in the 'man' directory. This does not seem to be the > case, however. > > Any help on how I should document the extended show method in this case, or > are there some mistakes in my original definition for extended 'show'? I > could not find suitable examples from the web/mailing lists. > > br > Leo > > > > On Fri, Jul 10, 2009 at 5:37 AM, Martin Morgan <mtmorgan@...> wrote: > >> L L wrote: >> > Dear all, >> > >> > I have been trying to create an R package. This has been successfull >> until I >> > tried to define classes. >> > >> > Currently, my procedure is the following: >> > >> > Start R, load the function and class definition >> >> tmp <- function (x) {x} >> >> setClass("rpa", contains = "list", where=topenv(parent.frame())) >> > >> > Use package skeleton to create directory structure: >> >> package.skeleton(name = "test",list=c("tmp")) >> >> I don't think package.skeleton knows to copy the class definition to the >> R files; I think you have to do that yourself -- is there a >> setClass("rpa", contains="list") instruction in the R files created by >> package.skeleton? >> >> Martin >> > >> > Edit man files, add test/NAMESPACE file with the following contents: >> >> export(tmp) >> >> exportClasses("rpa") >> > >> > Use R CMD check and R CMD build to create tarball. >> > -> No error messages. >> > >> > Start R, install the created package: >> >> install.packages("test_1.0.tar.gz",repos=NULL) >> > >> > Test the new package: >> >> require(test) >> >> my.object = new("rpa") >> > >> > This gives the error message >> >> Error in getClass(Class, where = topenv(parent.frame())) : >> >> "rpa" is not a defined class >> > >> > Any tips on how to define and export global classes in package creation? >> > >> > I also tried >> > - to replace the 'topenv(parent.frame())' with '.GlobalEnv' in setClass. >> > - place the setClass within function 'tmp' >> > - exportClasses(rpa) in the namespace (without suffixes "") >> > >> > I paged through the related mailing list discussions but could not find >> a >> > solution. Any tips and tricks would be worthwhile now; I guess this >> should >> > be a standard issue but it seems a bit tricky to come up with a quick >> > solution with the little programming experience that I have. >> > >> > kind regards >> > Leo Lahti >> > >> > [[alternative HTML version deleted]] >> > >> > ______________________________________________ >> > R-help@... mailing list >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE do read the posting guide >> http://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> >> > [[alternative HTML version deleted]] ______________________________________________ R-help@... mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
|
Re: class export in package creation / setClass / namespace?L L <lmlahti@...> writes:
> Ok, I could solve also the latter problem by defining show.myclass function in > the zzz.R file and adding the line 'S3method(show,myclass)' into NAMESPACE > file. Now the package passes all checks. I would have, in NAMESPACE, importFrom(methods, show) exportMethods(show) and in some file in R/ setMethod(show, "myclass", function(object) { cat("here I am\n") }) Do not try to make an S3 method on an S4 generic, or to create a new S4 generic for show. Since you've defined a method on show, you need to add documentation in a file in man/ (your choice; I'd add the documentation to the myclass documentation page). You only get one \alias{show,myclass-method}. Martin > The information on how to exactly extend existing methods and include new > methods/classes into a package is available but rather scattered in the web. A > step-by-step tutorial written by an experienced user who is aware of best > practices etc. would be rather useful for a beginner. > best regards > Leo > > > On Mon, Jul 20, 2009 at 7:09 PM, L L <[[lmlahti@...]]> wrote: > > Thanks, the issue was solved by adding class definitions to the > zzz.R file in the R code directory. However, this led to a new > problem. > The zzz.R now contains class definition: > > setClass("myclass", contains = "list") > and method definition for the new class, extending the generic > show': > > setGeneric("show",function(x,...){standardGeneric("show")}) > > setMethod("show", "myclass",function(x, ...) {cat("myclass object > \n")}) > I get two warnings. The first one: > * checking Rd files ... WARNING Rd files with duplicated alias > show,myclass-method': myclass-class.Rd show-methods.Rd > I get this one because the alias row for the method > (\alias{show,myclass-method}) is in both myclass-class.Rd file and > show-methods.Rd file (created by promptMethods function). This is > likely related to the second warning: > * checking for missing documentation entries ... WARNING Undocumented > code objects: show > I thought that the show method would've been documented correctly as > I put 'show-methods.Rd' file in the 'man' directory. This does not > seem to be the case, however. > Any help on how I should document the extended show method in this > case, or are there some mistakes in my original definition for > extended 'show'? I could not find suitable examples from the > web/mailing lists. > br Leo > > > > > > On Fri, Jul 10, 2009 at 5:37 AM, Martin Morgan > <[[mtmorgan@...]]> wrote: > > L L wrote: > Dear all, > > > I have been trying to create an R package. This has been > successfull until I > tried to define classes. > > > Currently, my procedure is the following: > > > Start R, load the function and class definition >> > tmp <- function (x) {x} >> setClass("rpa", > contains = "list", where=topenv(parent.frame())) > > > Use package skeleton to create directory structure: >> > package.skeleton(name = "test",list=c("tmp")) > > > > I don't think package.skeleton knows to copy the class > definition to the R files; I think you have to do > that yourself -- is there a setClass("rpa", > contains="list") instruction in the R files created by package.skeleton? > Martin > > > > Edit man files, add test/NAMESPACE > file with the following contents: >> export(tmp) >> > exportClasses("rpa") > > Use R CMD check > and R CMD build to create tarball. > -> No error > messages. > > Start R, install the > created package: >> > install.packages("test_1.0.tar.gz",repos=NULL) > > > Test the new package: >> require(test) >> > my.object = new("rpa") > > This gives > the error message >> Error in getClass(Class, > where = topenv(parent.frame())) : >> "rpa" is not > a defined class > > Any tips on how to > define and export global classes in package creation? > > > I also tried > - to replace the > topenv(parent.frame())' with '.GlobalEnv' in setClass. > > - place the setClass within function 'tmp' > - > exportClasses(rpa) in the namespace (without suffixes "") > > > I paged through the related mailing list discussions but > could not find a > solution. Any tips and tricks > would be worthwhile now; I guess this should > be > a standard issue but it seems a bit tricky to come up with > a quick > solution with the little programming > experience that I have. > > kind regards > > Leo Lahti > > > > > [[alternative HTML version deleted]] > > > ______________________________________________ > > [[R-help@...]] mailing list > > [[https://stat.ethz.ch/mailman/listinfo/r-help]] > > PLEASE do read the posting guide > [[http://www.R-project.org/posting-guide.html]] > > and provide commented, minimal, self-contained, > reproducible code. > > > > > > > > -- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 ______________________________________________ R-help@... mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
|
Re: class export in package creation / setClass / namespace?>>>>> "MartinMo" == Martin Morgan <mtmorgan@...>
>>>>> on Mon, 20 Jul 2009 18:57:33 -0700 writes: MartinMo> L L <lmlahti@...> writes: >> Ok, I could solve also the latter problem by defining show.myclass function in >> the zzz.R file and adding the line 'S3method(show,myclass)' into NAMESPACE >> file. Now the package passes all checks. MartinMo> I would have, in NAMESPACE, MartinMo> importFrom(methods, show) MartinMo> exportMethods(show) MartinMo> and in some file in R/ MartinMo> setMethod(show, "myclass", function(object) { MartinMo> cat("here I am\n") MartinMo> }) MartinMo> Do not try to make an S3 method on an S4 generic, MartinMo> or to create a new S4 generic for show. Since MartinMo> you've defined a method on show, you need to add MartinMo> documentation in a file in man/ (your choice; I'd MartinMo> add the documentation to the myclass documentation MartinMo> page). You only get one MartinMo> \alias{show,myclass-method}. MartinMo> Martin Yes, indeed! I'm just ``signing'' Martin Morgan's very good advice. In case it was explicit enough: do *NOT* define show.myclass() [which is an S3 method for an S4 generic and S4 class ..] Martin Mächler >> The information on how to exactly extend existing methods and include new >> methods/classes into a package is available but rather scattered in the web. A >> step-by-step tutorial written by an experienced user who is aware of best >> practices etc. would be rather useful for a beginner. >> best regards >> Leo >> >> >> On Mon, Jul 20, 2009 at 7:09 PM, L L <[[lmlahti@...]]> wrote: >> >> Thanks, the issue was solved by adding class definitions to the >> zzz.R file in the R code directory. However, this led to a new >> problem. >> The zzz.R now contains class definition: >> > setClass("myclass", contains = "list") >> and method definition for the new class, extending the generic >> show': >> > setGeneric("show",function(x,...){standardGeneric("show")}) > >> setMethod("show", "myclass",function(x, ...) {cat("myclass object >> \n")}) >> I get two warnings. The first one: >> * checking Rd files ... WARNING Rd files with duplicated alias >> show,myclass-method': myclass-class.Rd show-methods.Rd >> I get this one because the alias row for the method >> (\alias{show,myclass-method}) is in both myclass-class.Rd file and >> show-methods.Rd file (created by promptMethods function). This is >> likely related to the second warning: >> * checking for missing documentation entries ... WARNING Undocumented >> code objects: show >> I thought that the show method would've been documented correctly as >> I put 'show-methods.Rd' file in the 'man' directory. This does not >> seem to be the case, however. >> Any help on how I should document the extended show method in this >> case, or are there some mistakes in my original definition for >> extended 'show'? I could not find suitable examples from the >> web/mailing lists. >> br Leo ______________________________________________ R-help@... mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
|
|
Re: class export in package creation / setClass / namespace?Thanks to both Martins, your advice solved the confusion between S3 and S4
classes for the show method. br. Leo On Tue, Jul 21, 2009 at 9:58 AM, Martin Maechler <maechler@... > wrote: > >>>>> "MartinMo" == Martin Morgan <mtmorgan@...> > >>>>> on Mon, 20 Jul 2009 18:57:33 -0700 writes: > > MartinMo> L L <lmlahti@...> writes: > >> Ok, I could solve also the latter problem by defining show.myclass > function in > >> the zzz.R file and adding the line 'S3method(show,myclass)' into > NAMESPACE > >> file. Now the package passes all checks. > > MartinMo> I would have, in NAMESPACE, > > MartinMo> importFrom(methods, show) > MartinMo> exportMethods(show) > > MartinMo> and in some file in R/ > > MartinMo> setMethod(show, "myclass", function(object) { > MartinMo> cat("here I am\n") > MartinMo> }) > > MartinMo> Do not try to make an S3 method on an S4 generic, > MartinMo> or to create a new S4 generic for show. Since > MartinMo> you've defined a method on show, you need to add > MartinMo> documentation in a file in man/ (your choice; I'd > MartinMo> add the documentation to the myclass documentation > MartinMo> page). You only get one > MartinMo> \alias{show,myclass-method}. > > MartinMo> Martin > > Yes, indeed! > I'm just ``signing'' Martin Morgan's very good advice. > > In case it was explicit enough: > do *NOT* define show.myclass() [which is an S3 method for an > S4 generic and S4 class ..] > > Martin Mächler > > > >> The information on how to exactly extend existing methods and include > new > >> methods/classes into a package is available but rather scattered in > the web. A > >> step-by-step tutorial written by an experienced user who is aware of > best > >> practices etc. would be rather useful for a beginner. > >> best regards > >> Leo > >> > >> > >> On Mon, Jul 20, 2009 at 7:09 PM, L L <[[lmlahti@...]]> wrote: > >> > >> Thanks, the issue was solved by adding class definitions to the > >> zzz.R file in the R code directory. However, this led to a new > >> problem. > >> The zzz.R now contains class definition: > >> > setClass("myclass", contains = "list") > >> and method definition for the new class, extending the generic > >> show': > >> > setGeneric("show",function(x,...){standardGeneric("show")}) > > >> setMethod("show", "myclass",function(x, ...) {cat("myclass object > >> \n")}) > >> I get two warnings. The first one: > >> * checking Rd files ... WARNING Rd files with duplicated alias > >> show,myclass-method': myclass-class.Rd show-methods.Rd > >> I get this one because the alias row for the method > >> (\alias{show,myclass-method}) is in both myclass-class.Rd file and > >> show-methods.Rd file (created by promptMethods function). This is > >> likely related to the second warning: > >> * checking for missing documentation entries ... WARNING > Undocumented > >> code objects: show > >> I thought that the show method would've been documented correctly as > >> I put 'show-methods.Rd' file in the 'man' directory. This does not > >> seem to be the case, however. > >> Any help on how I should document the extended show method in this > >> case, or are there some mistakes in my original definition for > >> extended 'show'? I could not find suitable examples from the > >> web/mailing lists. > >> br Leo > > ______________________________________________ R-help@... mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. |
| Free embeddable forum powered by Nabble | Forum Help |