Re: [Rd] package: 'inline' to inline C or C++ code in R functions

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Wed, 23 May 2007 13:36:12 -0400

On 5/23/2007 11:52 AM, Oleg Sklyar wrote:
> Dear all,
>
> following the earlier discussion, I've made a couple of changes to
> 'inline' (http://www.ebi.ac.uk/~osklyar/inline). As announced before the
> idea of the package is to enable inlining C or C++ code in R functions
> using only R sources. The changes include:
>
> - it is possible to select between C and C++
> - it is possible to declare a standard R function calling a C/C++
> routine or an S4 method with a fixed signature
> - setCMethod accepts single of multiple method signatures in a list, all
> put in one shared object
> - readline syntax is used in the example for a multiline C++ source :)
>
> Thanks Duncan, Simon and Deepayan for useful comments!
>
> Not tested on Windows, I would be thankful if somebody could do it.

It doesn't quite work there. There are two problems: the test for windows should be based on .Platform$OS.type, and R CMD SHLIB expects filenames with Unix-style path separators. I've attached a patch.

Other suggestions: instead of relying on the filename for lookup when calling the compiled code, you might want to use getNativeSymbolInfo to obtain it right after compiling. This can be used as the name argument in .Call, but will avoid doing another lookup in the DLL, possibly finding a name clash.

You were asking how to handle automatic unloading: I haven't done this myself, but I think RODBC does something similar to close database connections.

Something else you might want to think about: if someone saves one of your cfunction results, you may need to recompile it when they reload.

Thanks for writing this, it's looking really nice.

Duncan Murdoch

diff -Naur inline/R/cfunction.R inline.new/R/cfunction.R

--- inline/R/cfunction.R	2007-05-23 10:42:00.000000000 -0400
+++ inline.new/R/cfunction.R	2007-05-23 13:13:38.625000000 -0400
@@ -23,11 +23,12 @@

   code <- paste( code, "\n warning(\"your C program does not return anything!\");\n return R_NilValue;\n}\n", sep="");    

   ## WRITE AND COMPILE THE CODE
- if ( length(grep("windows", tolower(version))) > 0 ) { + if ( .Platform$OS.type == "windows" ) {

     ## windows files

-    libCFile  <- paste(tempdir(), "\\", f, ".cpp", sep="")
-    libLFile  <- paste(tempdir(), "\\", f, ".dll", sep="")
-    libLFile2 <- paste(tempdir(), "\\", f, ".dll", sep="")
+    dir <- gsub("\\\\", "/", tempdir())
+    libCFile  <- paste(dir, "/", f, ".cpp", sep="")
+ libLFile <- paste(dir, "/", f, ".dll", sep="") + libLFile2 <- paste(dir, "/", f, ".dll", sep="")

   } else {

     ## UNIX-alike build
     libCFile  <- paste(tempdir(), "/", f, ".cpp", sep="")
diff -Naur inline/R/cmethods.R inline.new/R/cmethods.R
--- inline/R/cmethods.R	2007-05-23 11:29:54.000000000 -0400
+++ inline.new/R/cmethods.R	2007-05-23 13:18:34.015625000 -0400
@@ -15,12 +15,12 @@
     fSigs <- paste( "SEXP ", fIDs, "(", fSigs, ")", sep="")
     ## library c/cpp and so file ids
     libID <- basename(tempfile())

- if ( length(grep("windows", tolower(version))) > 0 ) { + if ( .Platform$OS.type == "windows" ) {
       ## windows files
-      if ( cpp )
-      libCFile  <- paste(tempdir(), "\\", libID, ".cpp", sep="")
-      libLFile  <- paste(tempdir(), "\\", libID, ".dll", sep="")
-      libLFile2 <- paste(tempdir(), "\\", libID, ".dll", sep="")
+      dir <- gsub("\\\\", "/", tempdir())
+      libCFile  <- paste(dir, "/", libID, ".cpp", sep="")
+      libLFile  <- paste(dir, "/", libID, ".dll", sep="")
+      libLFile2 <- paste(dir, "/", libID, ".dll", sep="")
     } else {
       ## UNIX-alike build
       libCFile  <- paste(tempdir(), "/", libID, ".cpp", sep="")

______________________________________________

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Wed 23 May 2007 - 17:35:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 23 May 2007 - 18:33:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.