[Rcpp-commits] r1387 - in pkg/Rcpp: R inst inst/doc inst/doc/Rcpp-modules inst/skeleton man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Jun 1 10:09:06 CEST 2010


Author: romain
Date: 2010-06-01 10:09:05 +0200 (Tue, 01 Jun 2010)
New Revision: 1387

Added:
   pkg/Rcpp/inst/skeleton/rcpp_module.cpp
   pkg/Rcpp/inst/skeleton/zzz.R
Modified:
   pkg/Rcpp/R/Rcpp.package.skeleton.R
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/doc/Makefile
   pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw
   pkg/Rcpp/man/Rcpp.package.skeleton.Rd
Log:
added example module in package skeleton

Modified: pkg/Rcpp/R/Rcpp.package.skeleton.R
===================================================================
--- pkg/Rcpp/R/Rcpp.package.skeleton.R	2010-05-31 20:00:02 UTC (rev 1386)
+++ pkg/Rcpp/R/Rcpp.package.skeleton.R	2010-06-01 08:09:05 UTC (rev 1387)
@@ -19,7 +19,8 @@
 	name = "anRpackage", list = character(), environment = .GlobalEnv,
 	path = ".", force = FALSE, namespace = TRUE, 
 	code_files = character(), 
-	example_code = TRUE ){
+	example_code = TRUE, 
+	module = FALSE ){
 	
 	env <- parent.frame(1)
 	
@@ -68,11 +69,17 @@
 	NAMESPACE <- file.path( root, "NAMESPACE")
 	if( file.exists( NAMESPACE ) ){
 		lines <- readLines( NAMESPACE )
+		ns <- file( NAMESPACE, open = "w" )
 		if( ! grepl( "useDynLib", lines ) ){
 			lines <- c( sprintf( "useDynLib(%s)", name), lines)
-			writeLines( lines, con = NAMESPACE )
+			writeLines( lines, con = nm )
 			message( " >> added useDynLib directive to NAMESPACE" )
 		}
+		
+		if(isTRUE(module)){
+			writeLines( 'importClassesFrom( Rcpp, "C++ObjectS3", "C++Object", "C++Class", "Module" )', nm )
+		}
+		close( ns )
 	}
 	
 	# lay things out in the src directory
@@ -107,6 +114,19 @@
 		writeLines( rcode , file.path( root, "R", "rcpp_hello_world.R" ) )
 		message( " >> added example R file calling the C++ example")
 	}
+	
+	if( isTRUE( module ) ){
+		file.copy( 
+			system.file( "skeleton", "rcpp_module.cpp", package = "Rcpp" ), 
+			file.path( root, "src" )
+		)
+		file.copy( 
+			system.file( "skeleton", "zzz.R", package = "Rcpp" ), 
+			file.path( root, "R" )
+		)
+		
+		message( " >> copied the example module " )
+	}
 	if( fake ){
 		rm( "Rcpp.fake.fun", envir = env )
 		unlink( file.path( root, "R"  , "Rcpp.fake.fun.R" ) )

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-05-31 20:00:02 UTC (rev 1386)
+++ pkg/Rcpp/inst/ChangeLog	2010-06-01 08:09:05 UTC (rev 1387)
@@ -1,3 +1,10 @@
+2010-06-01  Romain Francois <romain at r-enthusiasts.com>
+
+	* inst/doc/Rcpp-modules/Rcpp-modules.Rnw: document how to use modules
+	in other packages (namespace, etc ...)
+	
+	* R/Rcpp.package.skeleton: added the module argument
+
 2010-05-30  Romain Francois <romain at r-enthusiasts.com>
 
 	* R/Module.R: completion for C++ modules. 

Modified: pkg/Rcpp/inst/doc/Makefile
===================================================================
--- pkg/Rcpp/inst/doc/Makefile	2010-05-31 20:00:02 UTC (rev 1386)
+++ pkg/Rcpp/inst/doc/Makefile	2010-06-01 08:09:05 UTC (rev 1387)
@@ -34,7 +34,7 @@
 Rcpp-modules.pdf: Rcpp-modules/Rcpp-modules.Rnw
 	rm Rcpp-modules.Rnw 
 	cp -f Rcpp-modules/Rcpp-modules.Rnw .
-	Rscript -e "require(highlight); driver <- HighlightWeaveLatex(boxes = TRUE); Sweave( 'Rcpp-modules.Rnw', driver = driver ); "
+	Rscript -e "require(highlight); driver <- HighlightWeaveLatex(boxes = TRUE, bg = 'white' ); Sweave( 'Rcpp-modules.Rnw', driver = driver ); "
 	Rscript -e "tools::texi2dvi( 'Rcpp-modules.tex', pdf = TRUE, clean = FALSE )"
 	bibtex Rcpp-modules
 	Rscript -e "tools::texi2dvi( 'Rcpp-modules.tex', pdf = TRUE, clean = TRUE )"

Modified: pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw
===================================================================
--- pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw	2010-05-31 20:00:02 UTC (rev 1386)
+++ pkg/Rcpp/inst/doc/Rcpp-modules/Rcpp-modules.Rnw	2010-06-01 08:09:05 UTC (rev 1387)
@@ -13,6 +13,24 @@
 \author{Dirk Eddelbuettel \and Romain Fran\c{c}ois}
 \title{Exposing \proglang{C++} functions and classes with Rcpp modules}
 
+<<echo=FALSE>>=
+link <- function( f, package, text = f, root = "http://finzi.psych.upenn.edu/R/library/" ){
+	h <- if( missing(package) ) {
+		as.character( help( f ) )
+	} else {
+		as.character( help( f, package = paste( package, sep = "" ) ) )
+	}
+	if( ! length(h) ){
+		sprintf( "\\\\textbf{%s}", f )
+	} else {
+		rx <- "^.*/([^/]*?)/help/(.*?)$"
+		package <- sub( rx, "\\1", h, perl = TRUE )
+		page <- sub( rx, "\\2", h, perl = TRUE )
+		sprintf( "\\\\href{%s%s/html/%s.html}{\\\\texttt{%s}}", root, package, page, text )
+	}
+}
+@
+
 \begin{document}
 \maketitle
 
@@ -33,7 +51,7 @@
 by a consistent set of \proglang{C++} classes.
 
 However, these facilities are limited to a function by function basis. The 
-programmer has to implement a \texttt{.Call} compatible function
+programmer has to implement a \Sexpr{link(".Call")} compatible function
 using classes of the \pkg{Rcpp} API. 
 
 \subsection{Exposing functions}
@@ -181,7 +199,7 @@
 that exposes the \texttt{hello} function. \pkg{Rcpp} automatically 
 deduces the conversions that are needed for input and output. 
 
-On the R side, the module is simply retrieved by using the \texttt{Module}
+On the R side, the module is simply retrieved by using the \Sexpr{link("Module")}
 function from \pkg{Rcpp}:
 
 <<eval=FALSE>>=
@@ -350,6 +368,10 @@
 const_reference back ( ) const;
 @
 
+To resolve the ambiguity, it is possible to use \texttt{const\_method}
+or \texttt{nonconst\_method} instead of \texttt{method} in order 
+to restrict the candidate methods. 
+
 \subsubsection{S4 dispatch}
 
 When a \proglang{C++} class is exposed by the \texttt{class\_} template, 
@@ -432,6 +454,44 @@
 }                     
 @
 
+\section{Using modules in other packages}
+
+When using \pkg{Rcpp} modules in a packages, the client package needs to 
+import a set of classes from \pkg{Rcpp}. This is achieved by adding the 
+following line to the \texttt{NAMESPACE} file. 
+
+<<echo=FALSE,eval=TRUE>>=
+options( prompt = " ", continue = " " )
+@
+
+<<eval=FALSE>>=
+importClassesFrom( Rcpp, "C++ObjectS3", "C++Object", "C++Class", "Module" )
+@
+
+Loading modules that are defined in a package is best placed inside the 
+\Sexpr{link(".onLoad" )} hook for the package. 
+
+<<eval=FALSE>>=
+NAMESPACE <- environment()
+# this will be replaced by the real module
+yada <- new( "Module" )
+
+.onLoad <- function(libname, pkgname){
+	# load the module and store it in our namespace
+	unlockBinding( "yada" , NAMESPACE )
+	assign( "yada",  Module( "yada" ), NAMESPACE )
+	lockBinding( "yada", NAMESPACE )
+}
+@
+
+<<echo=FALSE,eval=TRUE>>=
+options( prompt = "> ", continue = "+ " )
+@
+
+The \Sexpr{link("Rcpp.package.skeleton")} function has been improved to help
+\pkg{Rcpp} modules. When the \texttt{module} argument is set to \texttt{TRUE}, 
+the skeleton generator installs code that uses a simple module. 
+
 \section{Future extensions}
 
 \texttt{Boost.Python} has many more features that we would like to port 

Added: pkg/Rcpp/inst/skeleton/rcpp_module.cpp
===================================================================
--- pkg/Rcpp/inst/skeleton/rcpp_module.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/skeleton/rcpp_module.cpp	2010-06-01 08:09:05 UTC (rev 1387)
@@ -0,0 +1,27 @@
+#include <Rcpp.h>
+
+class World {
+public:
+    World() : msg("hello"){}
+    void set(std::string msg) { this->msg = msg; }
+    std::string greet() { return msg; }
+
+private:
+    std::string msg;
+};
+
+void clearWorld( World* w){
+	w->set( "" ) ;
+}
+
+RCPP_MODULE(yada){
+	using namespace Rcpp ;
+	
+	class_<World>( "World" )
+		.method( "greet", &World::greet )
+		.method( "set", &World::set )
+		.method( "clear", &clearWorld )
+	;
+
+}            
+

Added: pkg/Rcpp/inst/skeleton/zzz.R
===================================================================
--- pkg/Rcpp/inst/skeleton/zzz.R	                        (rev 0)
+++ pkg/Rcpp/inst/skeleton/zzz.R	2010-06-01 08:09:05 UTC (rev 1387)
@@ -0,0 +1,14 @@
+
+# grab the namespace
+NAMESPACE <- environment()
+
+# dummy module, will be replace later
+yada <- new( "Module" )
+
+.onLoad <- function(pkgname, libname){
+	# load the module and store it in our namespace
+	unlockBinding( "yada" , NAMESPACE )
+	assign( "yada",  Module( "yada" ), NAMESPACE )
+	lockBinding( "yada", NAMESPACE )
+}
+

Modified: pkg/Rcpp/man/Rcpp.package.skeleton.Rd
===================================================================
--- pkg/Rcpp/man/Rcpp.package.skeleton.Rd	2010-05-31 20:00:02 UTC (rev 1386)
+++ pkg/Rcpp/man/Rcpp.package.skeleton.Rd	2010-06-01 08:09:05 UTC (rev 1387)
@@ -14,7 +14,7 @@
 Rcpp.package.skeleton(name = "anRpackage", list = character(), 
 	environment = .GlobalEnv, path = ".", force = FALSE, 
 	namespace = TRUE, code_files = character(), 
-	example_code = TRUE )
+	example_code = TRUE, module = FALSE )
 }
 \arguments{
 	\item{name}{See \link[utils]{package.skeleton}}
@@ -24,7 +24,8 @@
 	\item{force}{See \link[utils]{package.skeleton}}
 	\item{namespace}{See \link[utils]{package.skeleton}}
 	\item{code_files}{See \link[utils]{package.skeleton}}
-	\item{example_code}{If TRUE, example c++ code using Rcpp is added to the package}
+	\item{example_code}{If TRUE, example c++ code using Rcpp is added to the package. }
+	\item{module}{If TRUE, an example \code{\link{Module}} is added to the skeleton. }
 }
 \details{
 	In addition to \link[utils]{package.skeleton} : 



More information about the Rcpp-commits mailing list