[Rcpp-commits] r3914 - in pkg/Rcpp: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Nov 7 21:10:22 CET 2012


Author: jjallaire
Date: 2012-11-07 21:10:21 +0100 (Wed, 07 Nov 2012)
New Revision: 3914

Modified:
   pkg/Rcpp/ChangeLog
   pkg/Rcpp/R/Attributes.R
   pkg/Rcpp/man/cppFunction.Rd
Log:
sourceCpp depends parameter that respects Rcpp::interfaces generated include files

Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog	2012-11-07 19:13:08 UTC (rev 3913)
+++ pkg/Rcpp/ChangeLog	2012-11-07 20:10:21 UTC (rev 3914)
@@ -1,6 +1,8 @@
 2012-11-07  JJ Allaire <jj at rstudio.org>
 
-        * R/Attributes.R: derive depends from package LinkingTo
+        * R/Attributes.R: derive depends from package LinkingTo; change 
+        sourceCpp plugin parameter to depends and respect Rcpp::interfaces
+        generated include files
         * src/Attributes.cpp: validate exported C++ functions before calling;
         use static rather than inline for stubs to avoid call-site bloat;
         improved error message when package hpp already exists; import Rcpp
@@ -9,6 +11,7 @@
         * src/AttributesParser.cpp: add signature and isHidden methods
         * src/exceptions.cpp: add function_not_exported exception
         * include/Rcpp/exceptions.h: add function_not_exported exception
+        * man/cppFunction.Rd: update documentation on depends parameter
 
 2012-11-07  Romain Francois <romain at r-enthusiasts.com>
 

Modified: pkg/Rcpp/R/Attributes.R
===================================================================
--- pkg/Rcpp/R/Attributes.R	2012-11-07 19:13:08 UTC (rev 3913)
+++ pkg/Rcpp/R/Attributes.R	2012-11-07 20:10:21 UTC (rev 3914)
@@ -138,18 +138,18 @@
 
 # Define a single C++ function
 cppFunction <- function(code, 
-                        plugin = NULL,
-                        includes = NULL,
+                        depends = character(),
+                        includes = character(),
                         env = parent.frame(),
                         rebuild = FALSE,
                         showOutput = verbose,
                         verbose = getOption("verbose")) {
     
     # generate required scaffolding
-    if (!is.null(plugin)) {
-        depends <- paste(plugin, sep=", ")
+    if (!is.null(depends) && length(depends) > 0) {
+        depends <- paste(depends, sep=", ")
         scaffolding <- paste("// [[Rcpp::depends(", depends, ")]]", sep="")
-        scaffolding <- c(scaffolding, "", .rcppExportsIncludes(depends), 
+        scaffolding <- c(scaffolding, "", .linkingToIncludes(depends, FALSE), 
                          recursive=T)
     }
     else {
@@ -228,8 +228,9 @@
     cppFiles <- file.path(srcDir, cppFiles)
     cppFiles <- normalizePath(cppFiles, winslash = "/")
     
-    # generate the includes list based on LinkingTo
-    includes <- .rcppExportsIncludes(DESCRIPTION$LinkingTo)
+    # generate the includes list based on LinkingTo. Specify plugins-only
+    # because we only need as/wrap declarations
+    includes <- .linkingToIncludes(DESCRIPTION$LinkingTo, TRUE)
     
     # generate exports
     invisible(.Call("compileAttributes", PACKAGE="Rcpp", 
@@ -439,14 +440,12 @@
 }
 
 
-# Generate list of includes for RcppExports.cpp based on LinkingTo.
-# Since the RcppExports.cpp file contains only shims, we typically don't
-# need to capture headers from linked to packages. However, if a linked
-# to package includes definitions of Rcpp type converters (as/wrap) then
-# we do need those headers. To distinguish this case and to capture 
-# headers in the correct order we analyze the contents of the plugin's
-# includes field and extact the includes.before and includes.after
-.rcppExportsIncludes <- function(linkingTo) {
+# Generate list of includes based on LinkingTo. The pluginsOnly parameter
+# to distinguish the case of capturing all includes needed for a compiliation
+# (e.g. cppFunction) verses only needing to capture as/wrap converters which
+# is the case for generation of shims (RcppExports.cpp) and Rcpp::interfaces
+# package header files.
+.linkingToIncludes <- function(linkingTo, pluginsOnly) {
     
     # This field can be NULL or empty -- in that case just return Rcpp.h
     if (is.null(linkingTo) || !nzchar(linkingTo))
@@ -470,6 +469,16 @@
                 include.before <- c(include.before, includes$before)
                 include.after <- c(include.after, includes$after)
             }
+        } 
+        # otherwise check for standard Rcpp::interfaces generated include
+        else if (!pluginsOnly) {
+            pkgPath <- find.package(package, NULL, quiet=TRUE)
+            pkgHeader <- paste(package, ".hpp", sep="")
+            pkgHeaderPath <- file.path(pkgPath, "include",  pkgHeader)
+            if (file.exists(pkgHeaderPath)) {
+                pkgInclude <- paste("#include <", pkgHeader, ">", sep="")
+                include.after <- c(include.after, pkgInclude)
+            } 
         }
     }
     

Modified: pkg/Rcpp/man/cppFunction.Rd
===================================================================
--- pkg/Rcpp/man/cppFunction.Rd	2012-11-07 19:13:08 UTC (rev 3913)
+++ pkg/Rcpp/man/cppFunction.Rd	2012-11-07 20:10:21 UTC (rev 3914)
@@ -7,7 +7,7 @@
 Dynamically define an R function with C++ source code. Compiles and links a shared library with bindings to the C++ function then defines an R function that uses \code{.Call} to invoke the library.
 }
 \usage{
-cppFunction(code, plugin = NULL, includes = NULL, 
+cppFunction(code, depends = character(), includes = character(), 
             env = parent.frame(), rebuild = FALSE, 
             showOutput = verbose, verbose = getOption("verbose"))
 }
@@ -16,11 +16,11 @@
   \item{code}{
     Source code for the function definition.
 }
-  \item{plugin}{
-    The name of an \link[inline:plugins]{inline plugin} to use for the compilation.
+  \item{depends}{
+    Character vector of packages that the compilation depends on. Each package listed will first be queried for an \link[inline:plugins]{inline plugin} to determine header files to include. If no plugin is defined for the package then a header file based the package's name (e.g. \code{PkgName.hpp}) will be included.
 }
   \item{includes}{
-    User includes, inserted after the includes provided by the plugin.
+    Character vector of user includes (inserted after the includes provided by \code{depends}).
 }
   \item{env}{
     The \link[base:environment]{environment} in which to define the R function. May be \code{NULL} in which case the defined function can be obtained from the return value of \code{cppFunction}.
@@ -62,7 +62,7 @@
         return (fibonacci(x - 1)) + fibonacci(x - 2);
     }')
 
-cppFunction(plugin = "RcppArmadillo",
+cppFunction(depends = "RcppArmadillo",
     'List fastLm(NumericVector yr, NumericMatrix Xr) {
         
         int n = Xr.nrow(), k = Xr.ncol();



More information about the Rcpp-commits mailing list