[Rcpp-devel] namespace trick required

Romain Francois romain at r-enthusiasts.com
Thu Nov 25 13:31:47 CET 2010


Le 25/11/10 11:25, Romain Francois a écrit :
> Le 25/11/10 11:17, Romain Francois a écrit :
>> Hello,
>>
>> With formals<- I added into Rcpp yesterday, I have updated the parser
>> package as such:
>>
>>
>>
>> NAMESPACE <- environment()
>> MODULE <- Module( "parser_module" )
>>
>> nlines <- function( file ) NULL
>> count.chars <- function( file, encoding = "unknown" ) NULL
>>
>> .onLoad <- function( libname, pkgname ){
>> unlockBinding( "nlines" , NAMESPACE )
>> unlockBinding( "count.chars" , NAMESPACE )
>>
>> nlines <- MODULE$nlines
>> formals( nlines ) <- alist( file = )
>> assign( "nlines", nlines, NAMESPACE )
>>
>> count.chars <- MODULE$count.chars
>> formals( count.chars ) <- alist( file = , encoding = "unknown" )
>> assign( "count.chars", count.chars, NAMESPACE )
>>
>> lockBinding( "nlines", NAMESPACE )
>> lockBinding( "count.chars", NAMESPACE )
>> }
>>
>>
>> This is somewhat annoying that we have to first create dummies for
>> "nlines" and "count.chars", and then hijack them on the .onLoad.
>>
>> Can anyone think of another way ?
>
> Ah. This is what happens when you send a message too early. We actually
> don't need the dummies and can do:
>
> NAMESPACE <- environment()
> MODULE <- Module( "parser_module" )
>
> .onLoad <- function( libname, pkgname ){
>
> nlines <- MODULE$nlines
> formals( nlines ) <- alist( file = )
> assign( "nlines", nlines, NAMESPACE )
>
> count.chars <- MODULE$count.chars
> formals( count.chars ) <- alist( file = , encoding = "unknown" )
> assign( "count.chars", count.chars, NAMESPACE )
>
> lockBinding( "nlines", NAMESPACE )
> lockBinding( "count.chars", NAMESPACE )
> }
>
> I'm thinking about :
> - deal with the formals internally
> - add an R function that would dump functions and classes from a module
> into an environment or a namespace.

I've done both now and added the populate function to Rcpp. populate 
dumps the contents of a module into an environment (or a namespace).

The example above now reduces to:

NAMESPACE <- environment()
MODULE <- Module( "parser_module" )

.onLoad <- function( libname, pkgname ){
	populate( MODULE, NAMESPACE )
}


I'm not so sure populate is the right name, so I'm open to suggestions 
here.

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/9VOd3l : ZAT! 2010
|- http://bit.ly/c6DzuX : Impressionnism with R
`- http://bit.ly/czHPM7 : Rcpp Google tech talk on youtube




More information about the Rcpp-devel mailing list