Re: [Rd] Using R_MakeExternalPtr

From: Jonathan Zhou <>
Date: Wed, 25 Jul 2007 11:26:42 -0700 (PDT)

Hi Hin-Tak,

Here is the R code function in where I called the two C++ and further below are the 2 C++ functions I used to create the externalptr and use it :

soam.Rapply <- function (x, func, ...,




       njobs <- max(1,ceiling(nrow(x)/batch.size))

   if(!is.matrix(x) && !

       stop("x must be a matrix or data frame")


       {rowSet <- lapply(splitIndices(nrow(x), njobs), function(i) x[i, , drop = FALSE])} else {rowSet <- list(x)}

   sesCon <- .Call("soamInit")

   script <- " "

   fname <- tempfile(pattern = "Rsoam_data", tmpdir = getwd())    file(fname, open="w+")
   if(!is.null(savelist)) {

       dump(savelist, fname)


   for(counter in 1:length(packages))

       temp<-call("library", packages[counter], character.only=TRUE)
       dput(temp, fname)<-readLines(fname)

   for(counter in 1:njobs)

       caller <- paste("caller", counter, sep = "")<-call("dput", call("apply", X=rowSet[[counter]], MARGIN=1,
FUN=func), caller)
       dput(, fname)<-readLines(fname)

       final.script = temp[1]
       for(count in 2:length(temp)){
           final.script<-paste(final.script, temp[count], "\n")}

       .Call("soamSubmit", counter, sesCon, final.script, packages)

   .Call("soamGetResults", sesCon, njobs, join.method, parent.frame())

   for(job in 1:njobs)

       caller <- paste("result", job, sep = "")
       temp = dget(caller)
       if(job==1) {retval=temp} else {retval=join.method(retval,temp)}



extern "C"

SEXP soamInit ()

   // Initialize the API

   // Set up application specific information to be supplied to Symphony    char appName[] = "SampleAppCPP";

   // Set up application authentication information using the default security provider

   DefaultSecurityCallback securityCB("Guest", "Guest");

   // Connect to the specified application    ConnectionPtr conPtr = SoamFactory::connect(appName, &securityCB);

   // Set up session creation attributes    SessionCreationAttributes attributes;


   // Create a synchronous session
   Session* sesPtr = conPtr->createSession(attributes);

   SEXP out = R_MakeExternalPtr((void*)temp, R_NilValue, R_NilValue);

   return out;

extern "C"

 void soamSubmit       (SEXP jobID,            //job ID
                        SEXP sesCon,           //session pointer
                        SEXP caller,                   //objects
                        SEXP pack)                     //packages

char* savelist = CHAR(STRING_ELT(caller, 0)); string strTemp = ""; int job = INTEGER(jobID)[0]; void* temp = R_ExternalPtrAddr(sesCon); Session* sesPtr = reinterpret_cast<Session*>(temp); // Create a message MyMessage inMsg(job, /*pack,*/ savelist);

   // Send it
   TaskInputHandlePtr input = sesPtr->sendTaskInput(&inMsg); }

View this message in context:
Sent from the R devel mailing list archive at

______________________________________________ mailing list
Received on Wed 25 Jul 2007 - 18:30:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 26 Jul 2007 - 08:36:48 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.