[Rd] Creating a reference class object from a class definition in a package fails
John Chambers
jmc at r-project.org
Fri Feb 4 01:48:58 CET 2011
Yes, as noted in my previous mail, which crossed yours. If you included
the TestClass in a code_file= in the package skeleton, the default
NAMESPACE file from package.skeleton() will do that for you:
exportPattern("^[[:alpha:]]+")
exportClasses(
"TestClass"
It's certainly true that _if_ you have a namespace file, reference
classes (and all other classes) and generator objects have to be
exported to be used outside the package. Still not quite clear, though,
why _any_ namespace file is needed.
Thanks for the catch.
John
On 2/3/11 1:46 PM, Jeffrey Horner wrote:
> 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
>>
>
>
>
More information about the R-devel
mailing list