Skip to content

[Rcpp-devel] namespace trick required

3 messages · Romain Francois

#
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 ?

Romain
#
Le 25/11/10 11:17, Romain Francois a ?crit :
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.

Romain
#
Le 25/11/10 11:25, Romain Francois a ?crit :
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