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

2007-05-23 Thread Duncan Murdoch

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.R2007-05-23 10:42:00.0 -0400
+++ inline.new/R/cfunction.R2007-05-23 13:13:38.62500 -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.0 -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


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

2007-05-23 Thread Oleg Sklyar
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.

Duncan Murdoch wrote:
> Another suggestion:
> 
> You might want to allow a list of signatures and code chunks to be
> passed to the compiler, producing a list of functions to evaluate them,
> all compiled into one DLL.

Simon Urbanek wrote:
> I really like the idea! Except for the fact that it's forcing the use
> of C++ which adds unnecessary overhead :P I'd like a configurable
> extension [including .m] and the ability to prepend functions as code.

-- 
Dr Oleg Sklyar * EBI/EMBL, Cambridge CB10 1SD, England * +44-1223-494466

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel