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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to