From jmc at r-project.org  Fri Feb  4 01:38:31 2011
From: jmc at r-project.org (John Chambers)
Date: Thu, 03 Feb 2011 16:38:31 -0800
Subject: [Rd] Creating a reference class object from a class definition
 in a package fails
In-Reply-To: <AANLkTimXJ+J+HPR_+tPAExFUAJ3_xmYKYzQXvpn5r8du@mail.gmail.com>
References: <AANLkTimXJ+J+HPR_+tPAExFUAJ3_xmYKYzQXvpn5r8du@mail.gmail.com>
Message-ID: <4D4B4A87.4070801@r-project.org>

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
>



[Rd] Creating a reference class object from a class definition in a package fails
John Chambers