[R-pkg-devel] S3 length method behavior

Jim Lemon drjimlemon at gmail.com
Mon Feb 1 23:24:29 CET 2016


Hi Nathan,
Perhaps if you defined your "length" object as "nbm" (number of binary
messages), it would not interfere with the default "length".

Jim


On Tue, Feb 2, 2016 at 12:46 AM, Georgi Boshnakov <
georgi.boshnakov at manchester.ac.uk> wrote:

> Hi,
>
> >I have run into an issue while developing an R package. Specifically, my
> issue relates to what happens when I define an S3 length
> >method for my class. Here is my basic setup:
>
> ># Here is the structure of myClass
> ># uses an externalptr to operate on file using some C functions list(file
> = "my/file/location", handle = <externalptr>)
>
> ># this function gets an S3Method() in NAMESPACE length.myClass <-
> function(x) {
>  > .Call("get_class_length",myPkg)
> >}
>
> >As you can see, myClass is just a list of two pieces of information about
> the particular object. Without a defined length method, length(myClass)
> would be 2. However, I defined a length method to give me the information
> about the amount of binary messages contained in the file the object points
> to. In one of my test files there are 82 messages and length(myObject)
> correctly returns 82. What I ran into is that I was crashing RStudio when
> rebuilding my package with an object of myClass in my environment. Only
> > removing my S3 length method caused the crash to not occur. After
> looking for memory leaks and other errors I finally noticed that > the
> str() on the object of myClass looked odd. It returned something like this:
> > List of 82
> > $ file  : chr "my/file/location"
> > $ handle:<externalptr>
>  >$ NA:
> >Error in object[[i]] : subscript out of bounds
>
>
> You could define methods for indexing ("[" and "[[") for objects from your
> class.
> Since you have not defined special subsetting methods for your class, R
> uses a default one,  but honours your length(), hence the mess.
>
> Regards,
> Georgi Boshnkaov
>
>
> -----Original Message-----
> From: R-package-devel [mailto:r-package-devel-bounces at r-project.org] On
> Behalf Of Nathan Wendt
> Sent: 30 January 2016 19:25
> To: r-package-devel at r-project.org
> Subject: [R-pkg-devel] S3 length method behavior
>
> Hello,
>
> I have run into an issue while developing an R package. Specifically, my
> issue relates to what happens when I define an S3 length method for my
> class. Here is my basic setup:
>
> # Here is the structure of myClass
> # uses an externalptr to operate on file using some C functions list(file
> = "my/file/location", handle = <externalptr>)
>
> # this function gets an S3Method() in NAMESPACE length.myClass <-
> function(x) {
>   .Call("get_class_length",myPkg)
> }
>
> As you can see, myClass is just a list of two pieces of information about
> the particular object. Without a defined length method, length(myClass)
> would be 2. However, I defined a length method to give me the information
> about the amount of binary messages contained in the file the object points
> to. In one of my test files there are 82 messages and length(myObject)
> correctly returns 82. What I ran into is that I was crashing RStudio when
> rebuilding my package with an object of myClass in my environment. Only
> removing my S3 length method caused the crash to not occur. After looking
> for memory leaks and other errors I finally noticed that the str() on the
> object of myClass looked odd. It returned something like this:
>
> List of 82
>  $ file  : chr "my/file/location"
>  $ handle:<externalptr>
>  $ NA:
> Error in object[[i]] : subscript out of bounds
>
> It seems that when I define an S3 method for length that somehow the
> object of myClass then gets restructured to have the length returned by the
> function. There are 82 messages in the file, but myClass objects should
> only have two elements. My best guess here is that the RStudio crash was
> happening because the object was misrepresented internally. Of course, I
> cannot be sure. None of my tests pointed to anything useful.
>
> My questions are, then, whether this behavior makes sense and what to do
> about it. If I define my own str() method, will that fix it? I think I am
> just misunderstanding what is going on with the methods I have defined.
> Hopefully, someone can offer some clarity.
>
> Thanks,
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>
> ______________________________________________
> R-package-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list