Jeffrey Horner
2011-Feb-03 03:25 UTC
[Rd] Creating a reference class object from a class definition in a package fails
Hi, I'm trying to create a package that contains reference class definitions from which users can create reference objects, but there seems to be something awry. My toy example creates an empty package via package.skeleton('TestClass') to which I add the following R code: TestClass <- setRefClass('TestClass',fields=c('name')) Unfortunately my R console output bears this:> library(TestClass) > TestClass$new(name='foo')Error: attempt to apply non-function> getRefClass('TestClass')$new(name='foo')Error: attempt to apply non-function Creating the same reference class in the global environment works though:> x <- setRefClass('TestClass',fields='name') > x$new(name='foo')An object of class "TestClass" <environment: 0x82a43cc> I'm new to S4 and reference classes, so maybe I'm missing something fundamental? Jeff> sessionInfo()R version 2.13.0 Under development (unstable) (2011-02-02 r54197) Platform: i686-pc-linux-gnu (32-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices datasets utils methods base other attached packages: [1] TestClass_1.0 loaded via a namespace (and not attached): [1] tools_2.13.0
Jeffrey Horner
2011-Feb-03 21:46 UTC
[Rd] Creating a reference class object from a class definition in a package fails
Apparently reference classes must be declared in the NAMESPACE file via an S4 declaration. If I place the following in the NAMESPACE file all is well: exportClasses(TestClass) export(TestClass) Jeff On Wed, Feb 2, 2011 at 9:25 PM, Jeffrey Horner <jeffrey.horner at gmail.com> wrote:> Hi, > > I'm trying to create a package that contains reference class > definitions from which users can create reference objects, but there > seems to be something awry. > > My toy example creates an empty package via > package.skeleton('TestClass') to which I add the following R code: > > TestClass <- setRefClass('TestClass',fields=c('name')) > > Unfortunately my R console output bears this: > >> library(TestClass) >> TestClass$new(name='foo') > Error: attempt to apply non-function >> getRefClass('TestClass')$new(name='foo') > Error: attempt to apply non-function > > Creating the same reference class in the global environment works though: > >> x <- setRefClass('TestClass',fields='name') >> x$new(name='foo') > An object of class "TestClass" > <environment: 0x82a43cc> > > I'm new to S4 and reference classes, so maybe I'm missing something fundamental? > > Jeff > >> sessionInfo() > R version 2.13.0 Under development (unstable) (2011-02-02 r54197) > Platform: i686-pc-linux-gnu (32-bit) > > locale: > ?[1] LC_CTYPE=en_US.UTF-8 ? ? ? LC_NUMERIC=C > ?[3] LC_TIME=en_US.UTF-8 ? ? ? ?LC_COLLATE=en_US.UTF-8 > ?[5] LC_MONETARY=C ? ? ? ? ? ? ?LC_MESSAGES=en_US.UTF-8 > ?[7] LC_PAPER=en_US.UTF-8 ? ? ? LC_NAME=C > ?[9] LC_ADDRESS=C ? ? ? ? ? ? ? LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats ? ? graphics ?grDevices datasets ?utils ? ? methods ? base > > other attached packages: > [1] TestClass_1.0 > > loaded via a namespace (and not attached): > [1] tools_2.13.0 >-- http://biostat.mc.vanderbilt.edu/JeffreyHorner
John Chambers
2011-Feb-04 00:38 UTC
[Rd] Creating a reference class object from a class definition in a package fails
You don't say, but my guess is you created the package without a namespace. For reasons that are not too clear at the moment, the namespace seems to be needed. At any rate, replicating your experiment with the argument namespace=TRUE to package.skeleton() worked: > require(TestClass2) Loading required package: TestClass2 > TestClass$new() An object of class "TestClass" <environment: 0x100df2e38> Without that I replicated your result. Namespaces are a good idea anyway, though other things being equal it would be nice not to require them. For the moment, though, we do. John On 2/2/11 7:25 PM, Jeffrey Horner wrote:> Hi, > > I'm trying to create a package that contains reference class > definitions from which users can create reference objects, but there > seems to be something awry. > > My toy example creates an empty package via > package.skeleton('TestClass') to which I add the following R code: > > TestClass<- setRefClass('TestClass',fields=c('name')) > > Unfortunately my R console output bears this: > >> library(TestClass) >> TestClass$new(name='foo') > Error: attempt to apply non-function >> getRefClass('TestClass')$new(name='foo') > Error: attempt to apply non-function > > Creating the same reference class in the global environment works though: > >> x<- setRefClass('TestClass',fields='name') >> x$new(name='foo') > An object of class "TestClass" > <environment: 0x82a43cc> > > I'm new to S4 and reference classes, so maybe I'm missing something fundamental? > > Jeff > >> sessionInfo() > R version 2.13.0 Under development (unstable) (2011-02-02 r54197) > Platform: i686-pc-linux-gnu (32-bit) > > locale: > [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C > [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 > [5] LC_MONETARY=C LC_MESSAGES=en_US.UTF-8 > [7] LC_PAPER=en_US.UTF-8 LC_NAME=C > [9] LC_ADDRESS=C LC_TELEPHONE=C > [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C > > attached base packages: > [1] stats graphics grDevices datasets utils methods base > > other attached packages: > [1] TestClass_1.0 > > loaded via a namespace (and not attached): > [1] tools_2.13.0 > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Maybe Matching Threads
- Reference Classes: Generalizing Reference Class Generator objects?
- Reference Class error message: may be caused by lazy evaluation?
- Internally accessing ref class methods with .self$x is different from .self[['x']]
- Reference classes: accessor functions via 'getRefClass(...)$accessors(...)'
- Reference Classes: removing methods -> implications for objects/instances of that class