[Rd] problem packaging S4 class that contains a slot of jobjRef class

Adrian Dragulescu adrian_d at eskimo.com
Thu Nov 6 16:04:08 CET 2008



Hello,

I'm trying to package some source files that link R to a broker using
a Java API and the rJava package.  I am successful doing the connection
and operate on it using my R code.  I would like to package the files and
release the package.

I created some S4 classes to hold several Java objects.  I use WinXP and R
2.7.1.  I've searched RSeek and the Wiki for help, but could not find a
solution.

Here is my class definition:
twsConnect <- function(...)
  new("twsConnect", ...)

setClass(
  Class="twsConnect",
  representation=representation(
    clientId    = "integer",
    host        = "character",
    port        = "integer",
    reference   = "jobjRef"),
  prototype=prototype(
    clientId    = as.integer(0),
    host        = "",
    port        = as.integer(7496),
    reference   = .jnull())
)

setMethod("initialize", "twsConnect", function(
  .Object, clientId = 0, host = "", port = 7496, ...)
  {
    # hook up to the TWS
    ref <- .jnew("dev/AAD_Trader", as.integer(clientId), host,
                 as.integer(port))

    if (class(ref)[1] != "jobjRef")
      stop("Could not connect.  Check your settings.")

    .Object at clientId <- as.integer(clientId)
    .Object at host <- as.character(host)
    .Object at port <- as.integer(port)
    .Object at reference <- ref

    .Object
  }
)


setMethod("show", "twsConnect",
  function(object){
    cat("Object of class \"", class(object), "\":\n", sep="")
    lines <- NULL
    for (s in slotNames(object)){
      cont  <- slot(object, s)
      if (is.character(cont))cont <- paste('"', cont, '"', sep="")
      if (class(cont)=="jobjRef") cont <- "jobjRef"
      lines <- paste(lines, paste(s, " = ", cont, "\n",
                                  sep=""), sep="")
    }
    cat(lines)
  }
)



I have other files too.  Here is the output from package creation:
S:\All\Risk\Software\R\R-2.7.1\bin>Rcmd build --force --binary
H:/user/R/Adrian/RIB/
* checking for file 'H:/user/R/Adrian/RIB/DESCRIPTION' ... OK
* preparing 'H:/user/R/Adrian/RIB':
* checking DESCRIPTION meta-information ... OK
* removing junk files
* excluding invalid files from 'RIB'
Subdirectory 'R' contains invalid file names:
  .make.IB.package.R .utilities.R
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
WARNING: directory 'RIB/inst/doc' is empty
* building binary distribution
WARNING: some HTML links may not be found
installing R.css in C:/DOCUME~1/e47187/LOCALS~1/Temp/Rinst238480883

Using auto-selected zip options ''

---------- Making package RIB ------------
  adding build stamp to DESCRIPTION
  installing NAMESPACE file and metadata
  installing R files
  installing inst files
  preparing package RIB for lazy loading
Loading required package: rJava
Warning: package 'rJava' was built under R version 2.7.2
Loading required package: zoo

Attaching package: 'zoo'


        The following object(s) are masked from package:base :

         as.Date.numeric

Creating a new generic function for "summary" in "RIB"
  installing man source files
  installing indices
  installing help
 >>> Building/Updating help pages for package 'RIB'
     Formats: text html latex example chm
  IBrokers-package                  text    html    latex   example chm
  reqAccountUpdates                 text    html    latex   example chm
  reqHistoricalData                 text    html    latex   example chm
  twsConnect                        text    html    latex   example chm
Note: removing empty section \details
Note: removing empty section \details
  twsContract                       text    html    latex   example chm
  twsOrder                          text    html    latex   example chm
hhc: not found
CHM compile failed: HTML Help Workshop not installed?
  adding MD5 sums

packaged installation of package 'RIB' as RIB_0.1.0.zip
* DONE (RIB)

Not clean yet, but good for testing.

So, from R:

> require("RIB")
Loading required package: RIB
Loading required package: rJava
Loading required package: zoo

Attaching package: 'zoo'


        The following object(s) are masked from package:base :

         as.Date.numeric

Warning message:
package 'rJava' was built under R version 2.7.2
> tws <- twsConnect()
> tws
Object of class "twsConnect":
clientId = 0
host = ""
port = 7496
reference = jobjRef
> tws at reference
[1] "Java-Object<null>"
So I have no connection...

> tws <- new("twsConnect", 1, "", 7496)
Error in initialize(value, ...) :
  cannot use object of class "numeric" in new():  class "twsConnect" does
not extend that class
>

but if I source the file:
> source("H:/user/R/Adrian/RIB/R/twsConnect.R")
> tws <- twsConnect()
> tws
Object of class "twsConnect":
clientId = 0
host = ""
port = 7496
reference = jobjRef
>
> tws at reference
[1] "Java-Object{dev.AAD_Trader at 12558d6}"
>
Things work as I want, I have the connection.  What does source do, that I
miss when packaging?

My NAMESPACE file is:
exportPattern("^[^\\.]")

exportClasses("twsConnect", "twsContract", "twsExecutionFilter",
              "twsOrder")

exportMethods("show")

My .onLoad function contains ".jpackage(pkgname)".

I tried to package just the class twsConnect without the slot reference,
and things worked out fine.  I tried to say that twsConnect contains
"jobjRef" but did not have much success.


Thanks a lot,
Adrian Dragulescu



More information about the R-devel mailing list