PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Doc/Manual/R.html

#
HTML | 180 lines | 135 code | 43 blank | 2 comment | 0 complexity | 576bdabafab6ca1224e0268ed1e53705 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>SWIG and R</title>
  5. <link rel="stylesheet" type="text/css" href="style.css">
  6. </head>
  7. <body bgcolor="#ffffff">
  8. <H1><a name="R"></a>35 SWIG and R</H1>
  9. <!-- INDEX -->
  10. <div class="sectiontoc">
  11. <ul>
  12. <li><a href="#R_nn2">Bugs</a>
  13. <li><a href="#R_nn3">Using R and SWIG</a>
  14. <li><a href="#R_nn4">Precompiling large R files</a>
  15. <li><a href="#R_nn5">General policy</a>
  16. <li><a href="#R_language_conventions">Language conventions</a>
  17. <li><a href="#R_nn6">C++ classes</a>
  18. <li><a href="#R_nn7">Enumerations</a>
  19. </ul>
  20. </div>
  21. <!-- INDEX -->
  22. <p>
  23. R is a GPL'ed open source statistical and plotting environment.
  24. Information about R can be found at <a
  25. href="http://www.r-project.org/">www.r-project.org</a>.
  26. The R bindings are under active development. They have been used to
  27. compile and run an R interface to QuantLib running on Mandriva Linux
  28. with gcc. The R bindings also work on Microsoft Windows using Visual C++.
  29. </p>
  30. <H2><a name="R_nn2"></a>35.1 Bugs</H2>
  31. <p>
  32. Currently the following features are not implemented or broken:
  33. </p>
  34. <ul>
  35. <li>Garbage collection of created objects
  36. <li>C Array wrappings
  37. </ul>
  38. <H2><a name="R_nn3"></a>35.2 Using R and SWIG</H2>
  39. <p>
  40. To use R and SWIG in C mode, execute the following commands where
  41. example.c is the name of the file with the functions in them
  42. </p>
  43. <div class="shell">
  44. <pre>
  45. swig -r example.i
  46. R CMD SHLIB example_wrap.c example.c
  47. </pre>
  48. </div>
  49. <p>
  50. The corresponding options for C++ mode are
  51. </p>
  52. <div class="shell">
  53. <pre>
  54. swig -c++ -r -o example_wrap.cpp example.i
  55. R CMD SHLIB example_wrap.cpp example.cpp
  56. </pre>
  57. </div>
  58. <p>
  59. Note that R is sensitive to the names of the files.
  60. The name of the wrapper file must be the
  61. name of the library unless you use the -o option to R when building the library, for example:
  62. </p>
  63. <div class="shell">
  64. <pre>
  65. swig -c++ -r -o example_wrap.cpp example.i
  66. R CMD SHLIB -o example.so example_wrap.cpp example.cpp
  67. </pre>
  68. </div>
  69. <p>
  70. R is also sensitive to the name of the file
  71. extension in C and C++ mode. In C++ mode, the file extension must be .cpp
  72. rather than .cxx for the R compile command to recognize it. If your C++ code is
  73. in a file using something other than a .cpp extension, then it may still work using PKG_LIBS:
  74. </p>
  75. <div class="shell">
  76. <pre>
  77. swig -c++ -r -o example_wrap.cpp example.i
  78. PKG_LIBS="example.cxx" R CMD SHLIB -o example example_wrap.cpp
  79. </pre>
  80. </div>
  81. <p>
  82. The commands produces two files. A dynamic shared object file called
  83. example.so, or example.dll, and an R wrapper file called example.R. To load these
  84. files, start up R and type in the following commands
  85. </p>
  86. <div class="shell">
  87. <pre>
  88. dyn.load(paste("example", .Platform$dynlib.ext, sep=""))
  89. source("example.R")
  90. cacheMetaData(1)
  91. </pre>
  92. </div>
  93. The cacheMetaData(1) will cause R to refresh its object tables.
  94. Without it, inheritance of wrapped objects may fail.
  95. <p>
  96. These two files can be loaded in any order
  97. </p>
  98. <H2><a name="R_nn4"></a>35.3 Precompiling large R files</H2>
  99. In cases where the R file is large, one make save a lot of loading
  100. time by precompiling the R wrapper. This can be done by creating the
  101. file makeRData.R which contains the following
  102. <pre>
  103. source('BigFile.R')
  104. save(list=ls(all=TRUE),file="BigFile.RData", compress=TRUE)
  105. q(save="no")
  106. </pre>
  107. This will generate a compiled R file called BigFile.RData that
  108. will save a large amount of loading time.
  109. <H2><a name="R_nn5"></a>35.4 General policy</H2>
  110. <p>
  111. The general policy of the module is to treat the C/C++ as a basic
  112. wrapping over the underlying functions and rely on the R type system
  113. to provide R syntax.
  114. </p>
  115. <H2><a name="R_language_conventions"></a>35.5 Language conventions</H2>
  116. <p>
  117. getitem and setitem use C++ conventions (i.e. zero based indices). [<-
  118. and [ are overloaded to allow for R syntax (one based indices and
  119. slices)
  120. </p>
  121. <H2><a name="R_nn6"></a>35.6 C++ classes</H2>
  122. <p>
  123. C++ objects are implemented as external pointer objects with the class
  124. being the mangled name of the class. The C++ classes are encapsulated
  125. as an SEXP with an external pointer type. The class is the mangled
  126. name of the class. The nice thing about R is that is allows you to
  127. keep track of the pointer object which removes the necessity for a lot
  128. of the proxy class baggage you see in other languages.
  129. </p>
  130. <H2><a name="R_nn7"></a>35.7 Enumerations</H2>
  131. <p>
  132. enumerations are characters which are then converted back and forth to
  133. ints before calling the C routines. All of the enumeration code is
  134. done in R.
  135. </p>
  136. </body>
  137. </html>