Committing R-SWIG

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9175 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Joseph Wang 2006-06-29 03:01:18 +00:00
commit 3a821460fe
51 changed files with 5154 additions and 9 deletions

91
SWIG/Doc/Manual/R.html Normal file
View file

@ -0,0 +1,91 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>SWIG and R</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="#ffffff">
<H1>SWIG and R</H1>
<p>
R is a GPL'ed open source statistical and plotting environment.
Information about R can be found at <a
href="http://www.r-project.org/">www.r-project.org</a>.
The R binding are under active development and are extremely
experimental. Not all features have been implemented and the API is
not stable.
</p>
<H2>Bugs</H2>
<p>
Currently the following features are not implemented or broken:
<ul>
<li>Garbage collection of created objects
<li>C Array wrappings
<li>tested on UNIX only, how well or badly it works on windows is not known
</ul>
<H2>Using R and SWIG</H2>
<p>
To use R and SWIG in C mode, execute the following commands where
example_func.c is the name of the file with the functions in them
<div class="shell">
<pre>
swig -r -o example,c example.i
PKG_LIBS="example_func.c" R CMD SHLIB example.c
</pre>
</div>
The corresponding comments for C++ mode are
<div class="shell">
<pre>
swig -c++ -r -o example.cpp example.i
PKG_LIBS="example_func.cxx" R CMD SHLIB example.cpp
</pre>
</div>
Note that R is sensitive to the name of the file and to the file
extension in C and C++ mode. The name of the wrapper file must be the
name of the library. Also in C++ mode, the file extension must be cpp
rather than cxx for the R compile command to recognize it.
</p>
<p>
The commands produce two files. A dynamic shared object file called
example.so and an R wrapper file called example_wrap.S. To load these
files, start up R and type in the following commands
<div class="shell">
<pre>
dyn.load('example.so')
source('example_wrap.S')
</pre>
These two files can be loaded in any order
</p>
<h2>General policy</h2>
The general policy of the module is to treat the C/C++ as a basic
wrapping over the underlying functions and rely on the R type system
to provide R syntax.
<h2>Language conventions</h2>
getitem and setitem use C++ conventions (i.e. zero based indices). [<-
and [ are overloaded to allow for R syntax (one based indices and
slices)
<h2>C++ classes</h2>
C++ objects are implemented as external pointer objects with the class
being the mangled name of the class. The C++ classes are encapsulated
as an SEXP with an external pointer type. The class is the mangled
name of the class. The nice thing about R is that is allows you to
keep track of the pointer object which removes the necessity for a lot
of the proxy class baggage you see in other languages.
<h2>Enumerations</h2>
enumerations are characters which are then converted back and forth to
ints before calling the C routines. All of the enumeration code is
done in R.
</body>
</html>

View file

@ -29,4 +29,5 @@ Pike.html
Python.html
Ruby.html
Tcl.html
R.html
Extending.html

View file

@ -1017,3 +1017,26 @@ uffi_clean:
rm -f core @EXTRA_CLEAN@
rm -f *.@OBJEXT@ *@SO@
##################################################################
##### R ######
##################################################################
R = R
RSRCS = $(INTERFACE:.i=_wrap.c) #special code for R build system
RCXXSRCS = $(INTERFACE:.i=_wrap.cpp) #special code for R build system
r: $(SRCS)
$(SWIG) -r $(SWIGOPT) -o $(RSRCS) $(INTERFACE)
PKG_LIBS="$(SRCS)" PKG_CPPFLAGS="$(INCLUDES)" \
$(R) CMD SHLIB $(RSRCS)
r_cpp: $(CXXSRCS)
$(SWIG) -c++ -r $(SWIGOPT) -o $(RCXXSRCS) $(INTERFACE)
PKG_LIBS="$(CXXSRCS)" PKG_CPPFLAGS="$(INCLUDES)" \
$(R) CMD SHLIB $(RCXXSRCS)
r_clean:
rm -f *_wrap* *~ .~* $(RSRCS) $(RCXXSRCS)
rm -f core @EXTRA_CLEAN@
rm -f *.@OBJEXT@ *@SO@

View file

@ -0,0 +1,18 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
LIBS = -lm
ARGS = SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' r_cpp
clean::
$(MAKE) -f $(TOP)/Makefile $(ARGS) r_clean
check: all
R CMD BATCH runme.R

View file

@ -0,0 +1,28 @@
/* File : example.c */
#include "example.h"
#define M_PI 3.14159265358979323846
/* Move the shape to a new location */
void Shape::move(double dx, double dy) {
x += dx;
y += dy;
}
int Shape::nshapes = 0;
double Circle::area(void) {
return M_PI*radius*radius;
}
double Circle::perimeter(void) {
return 2*M_PI*radius;
}
double Square::area(void) {
return width*width;
}
double Square::perimeter(void) {
return 4*width;
}

View file

@ -0,0 +1,39 @@
/* File : example.h */
class Shape {
public:
Shape() {
nshapes++;
}
virtual ~Shape() {
nshapes--;
};
double x, y;
void move(double dx, double dy);
virtual double area(void) = 0;
virtual double perimeter(void) = 0;
static int nshapes;
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) { };
virtual double area(void);
virtual double perimeter(void);
};
class Square : public Shape {
private:
double width;
public:
Square(double w) : width(w) { };
virtual double area(void);
virtual double perimeter(void);
};

View file

@ -0,0 +1,9 @@
/* File : example.i */
%module example
%inline %{
#include "example.h"
%}
%include "example.h"

View file

@ -0,0 +1,49 @@
# This file illustrates the shadow-class C++ interface generated
# by SWIG.
dyn.load('example_wrap.so')
source('example_wrap.R')
cacheMetaData(1)
# ----- Object creation -----
print("Creating some objects:")
circle <- Circle(10)
print (" Created circle")
square <- Square(10)
print (" Created square")
# ----- Access a static member -----
sprintf("A total of %d shapes were created", Shape_nshapes())
# ----- Member data access -----
# Set the location of the object
circle$x <- 20
circle$y <- 30
square$x <- -10
square$y <- 5
print("Here is their current position:")
sprintf(" Circle = (%f, %f)", circle$x,circle$y)
sprintf(" Square = (%f, %f)", square$x,square$y)
# ----- Call some methods -----
print ("Here are some properties of the shapes:")
sapply(c(circle, square),
function(o) {
sprintf(" area = %f perimeter = %f", o$area(), o$perimeter())
})
print("Guess I'll clean up now")
delete(circle)
delete(square)
sprintf("%d shapes remain", Shape_nshapes())
print ("Goodbye");

View file

@ -0,0 +1,16 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
TARGET = example
INTERFACE = example.i
ARGS = SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
all::
$(MAKE) -f $(TOP)/Makefile $(ARGS) r
clean::
$(MAKE) -f $(TOP)/Makefile $(ARGS) r_clean
check: all
R CMD BATCH runme.R

View file

@ -0,0 +1,18 @@
/* File : example.c */
/* A global variable */
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
int g;
g = y;
while (x > 0) {
g = x;
x = y % x;
y = g;
}
return g;
}

View file

@ -0,0 +1,7 @@
/* File : example.i */
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}

View file

@ -0,0 +1,24 @@
# file: runme.R
dyn.load('example_wrap.so')
source('example_wrap.R')
cacheMetaData(1)
# Call our gcd() function
x <- 42
y <- 105
g <- gcd(x,y)
sprintf("The gcd of %d and %d is %d", x, y, g)
# Manipulate the Foo global variable
# Output its current value
Foo()
# Change its value
Foo(3.1415926)
# See if the change took effect
Foo()

View file

@ -35,7 +35,7 @@ CXXSRCS =
CSRCS =
TARGETPREFIX =
TARGETSUFFIX =
SWIGOPT = -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$(LANGUAGE) -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)
SWIGOPT = -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$(LANGUAGE) -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE) -DSWIG_NOEXTRA_QUALIFICATION
INCLUDES = -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$(LANGUAGE) -I$(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)
LIBS = -L.
LIBPREFIX = lib

View file

@ -77,7 +77,7 @@ public:
SWIG_STD_VECTOR_SPECIALIZE_MINIMUM(Flow, Space::Flow)
#endif
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON)
#if defined(SWIGJAVA) || defined(SWIGCSHARP) || defined(SWIGPYTHON)||defined(SWIGR)
#define SWIG_GOOD_VECTOR
%ignore std::vector<Space::Flow>::vector(size_type);
%ignore std::vector<Space::Flow>::resize(size_type);

View file

@ -19,7 +19,11 @@
int x;
int *xp;
#ifdef SWIGR
int& c_member = x;
#else
int& c = x;
#endif
void *vp;

View file

@ -61,7 +61,10 @@ namespace std
%inline {
/* silently rename the parameter names in csharp/java */
#ifdef SWIGR
double foo(double inparam, double out) { return 1.0; }
#else
double foo(double in, double out) { return 1.0; }
#endif
double bar(double native, bool boolean) { return 1.0; }
}

View file

@ -0,0 +1,54 @@
#######################################################################
# $Header$
# Makefile for mzscheme test-suite
#######################################################################
LANGUAGE = r
SCRIPTSUFFIX = _runme.R
WRAPSUFFIX = _wrap.R
RUNR = R CMD BATCH --no-save --no-restore
srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
C_TEST_CASES = copyStruct simpleArray legacy
CPP_TEST_CASES = funcptr double_delete
include $(srcdir)/../common.mk
# Overridden variables here
# Rules for the different types of tests
%.cpptest:
$(setup) \
($(swig_and_compile_cpp); ) && \
$(run_testcase)
%.ctest:
$(setup) \
($(swig_and_compile_c); ) && \
$(run_testcase)
%.multicpptest:
$(setup) \
($(swig_and_compile_multi_cpp); ) && \
$(run_testcase)
# Runs the testcase.
#
# Run the runme if it exists. If not just load the R wrapper to
# check for syntactic correctness
run_testcase = \
if [ -f $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then ( \
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH \
$(RUNR) $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ;) \
else \
($(RUNR) $(srcdir)/$(SCRIPTPREFIX)$*$(WRAPSUFFIX);) \
fi;
# Clean
clean:
$(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile r_clean
%.clean:
@rm -f $*_wrap.R $*_wrap.so $*_wrap.cpp $*_wrap.c $*_runme.Rout

View file

@ -0,0 +1,55 @@
source('unittest.R')
dyn.load('copyStruct_wrap.so')
source('copyStruct_wrap.R')
cacheMetaData(1)
a <- getA()
r = getARef()
unittest(A_d_get(r), 42)
unittest(r$d, 42)
unittest(r$i, 20)
# An error in trying to access a field that doesn't exist.
try(r$foo)
r$d <- pi
unittesttol(r$d, 3.141593, 0.0001)
r$i <- -100
r$ui
r$ui <- 10
# An error since i is unsigned and so must be positive.
try(r$ui <- -10)
a = A()
unittest(a$i,0)
unittest(a$d,0)
unittest(a$ui,0)
a$ui <- 100
unittest(a$ui,100)
a$d = 1
unittest(a$d,1)
d <- bar()
unittest(class(d), "_p_D")
unittest(d$x, 1)
unittest(d$u, 0)
la <- new("A");
la@ui <- 5
other = A()
foo <- copyToC(la, other)
aa = A()
aa$i = 201
aa$d = pi
aa$str = 'foo'
copyToR(aa)

View file

@ -0,0 +1,15 @@
/* File : example.i */
%module double_delete
%inline %{
class Foo {
private:
double r;
public:
Foo(double rin) : r(rin) {};
};
%}

View file

@ -0,0 +1,12 @@
# This file illustrates the shadow-class C++ interface generated
# by SWIG.
dyn.load('double_delete_wrap.so')
source('double_delete_wrap.R')
cacheMetaData(1)
# ----- Object creation -----
f <- Foo(2.0)
delete(f);
delete(f);

Some files were not shown because too many files have changed in this diff Show more